feat: users & authentication implementation

This commit is contained in:
Florian Sylvain
2023-08-07 12:31:29 +02:00
parent e258dccc1c
commit f33f53ca76
15 changed files with 550 additions and 18 deletions
+20 -4
View File
@@ -6,6 +6,7 @@ import (
"fmt"
"github.com/go-chi/chi/v5"
"github.com/go-chi/httplog"
"github.com/go-chi/jwtauth/v5"
"net/http"
)
@@ -16,15 +17,30 @@ func jsonContentTypeMiddleware(next http.Handler) http.Handler {
})
}
func initJwt() {
api.TokenAuth = jwtauth.New("HS256", []byte("secret"), nil)
}
func getHelloWorld(w http.ResponseWriter, _ *http.Request) {
msg, _ := json.Marshal(map[string]string{"message": "Hello World"})
_, _ = w.Write(msg)
}
func main() {
api.InitContainer()
api.InitValidator()
initJwt()
r := chi.NewRouter()
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
msg, _ := json.Marshal(map[string]string{"message": "Hello World"})
_, _ = w.Write(msg)
r.Get("/", getHelloWorld)
r.Group(func(r chi.Router) {
r.Use(jwtauth.Verifier(api.TokenAuth))
r.Use(jwtauth.Authenticator)
r.Mount("/article", api.NewArticleRouter())
})
r.Mount("/article", api.NewArticleRouter())
r.Mount("/auth", api.NewAuthRouter())
apiRouter := chi.NewRouter()
apiRouter.Use(httplog.RequestLogger(httplog.NewLogger("GohCMS2")))