Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion internal/server/admin/users/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func (h *Handler) Register(r fiber.Router) {
admin := r.Group(
"/admin/users",
h.errorsHandler,
jwtauth.New(h.jwtSvc, h.usersSvc),
jwtauth.WithRole(users.RoleAdmin),
)

Expand Down
1 change: 0 additions & 1 deletion internal/server/auth/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func (h *Handler) Register(r fiber.Router) {
auth.Post("/login", validation.DecorateWithBodyEx(h.Validator, h.handleLogin))
auth.Post(
"/change-password",
jwtauth.New(h.jwtSvc, h.usersSvc),
validation.DecorateWithBodyEx(h.Validator, h.handleChangePassword),
)
}
Expand Down
5 changes: 5 additions & 0 deletions internal/server/middlewares/jwtauth/jwtauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package jwtauth
import (
"errors"
"fmt"
"strings"

"github.com/bit-issues/backend/internal/jwt"
"github.com/bit-issues/backend/internal/users"
Expand All @@ -19,6 +20,10 @@ const (
// New returns a middleware that validates JWT token and sets user info in context.
func New(jwtSvc *jwt.Service, usersSvc *users.Service) fiber.Handler {
return keyauth.New(keyauth.Config{
Next: func(c *fiber.Ctx) bool {
path := strings.TrimRight(c.Path(), "/")
return path == "/api/v1/auth/login" || path == "/api/v1/auth/register"
},
Comment thread
capcom6 marked this conversation as resolved.
Validator: func(c *fiber.Ctx, token string) (bool, error) {
claims, err := jwtSvc.ValidateToken(token)
if err != nil {
Expand Down
10 changes: 8 additions & 2 deletions internal/server/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func Module() fx.Option {
}),
fx.Supply(docs.SwaggerInfo),

fx.Provide(
fx.Annotate(jwtauth.New, fx.ResultTags(`name:"jwtauth"`)),
fx.Private,
),

fx.Provide(
fx.Annotate(users.NewHandler, fx.ResultTags(`group:"handlers"`)),
fx.Annotate(auth.NewHandler, fx.ResultTags(`group:"handlers"`)),
Expand All @@ -45,7 +50,7 @@ func Module() fx.Option {

fx.Invoke(
fx.Annotate(
func(handlers []handler.Handler, healthHandler *health.Handler, openapiHandler *openapi.Handler, app *fiber.App) {
func(handlers []handler.Handler, jwtAuth fiber.Handler, healthHandler *health.Handler, openapiHandler *openapi.Handler, app *fiber.App) {
// Health endpoint
healthHandler.Register(app)

Expand All @@ -55,14 +60,15 @@ func Module() fx.Option {

v1.Use(
validation.Middleware,
jwtAuth,
jwtauth.ErrorsHandler(),
)
Comment thread
capcom6 marked this conversation as resolved.

for _, h := range handlers {
h.Register(v1)
}
},
fx.ParamTags(`group:"handlers"`),
fx.ParamTags(`group:"handlers"`, `name:"jwtauth"`),
),
),
)
Expand Down
1 change: 0 additions & 1 deletion internal/server/projects/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ func (h *Handler) Register(r fiber.Router) {
projects := r.Group(
"/projects",
h.errorsHandler,
jwtauth.New(h.jwtSvc, h.usersSvc),
)

projects.Get("/", h.list)
Expand Down
Loading