refactor!: Use gin router instead chi router
- Removed chi dependencies - Updated router registration logic
This commit is contained in:
@@ -4,30 +4,39 @@ import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"git.ostiwe.com/ostiwe-com/status/modules/jwt"
|
||||
"git.ostiwe.com/ostiwe-com/status/repository"
|
||||
"github.com/go-chi/jwtauth/v5"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SetUserFromJWT(next http.Handler) http.Handler {
|
||||
fn := func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
token, _, err := jwtauth.FromContext(ctx)
|
||||
func SetUserFromJWT() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
token, err := jwt.AuthMiddleware.ParseToken(c)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
c.Writer.WriteHeader(http.StatusUnauthorized)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
userRepo := repository.NewUserRepository()
|
||||
user, err := userRepo.FindByLogin(ctx, token.Subject())
|
||||
subject, err := token.Claims.GetSubject()
|
||||
if err != nil {
|
||||
c.Writer.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := userRepo.FindByLogin(ctx, subject)
|
||||
if err != nil || user == nil {
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
c.Writer.WriteHeader(http.StatusUnauthorized)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx = context.WithValue(ctx, "user", user)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
|
||||
c.Next()
|
||||
}
|
||||
return http.HandlerFunc(fn)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user