sonarqube-badges/router/utils/verify.go
2025-03-15 01:49:52 +01:00

29 lines
659 B
Go

package utils
import (
"github.com/golang-jwt/jwt/v5"
"gorm.io/gorm"
"net/http"
"sonarqube-badge/config"
"sonarqube-badge/store"
)
func VerifyUser(w http.ResponseWriter, r *http.Request) (*gorm.DB, config.Config, *store.User, bool) {
ctx := r.Context()
cfg := ctx.Value("config").(config.Config)
db := ctx.Value("db").(*gorm.DB)
token, err := GetToken(r)
if err != nil {
w.WriteHeader(http.StatusForbidden)
return nil, cfg, nil, true
}
email := token.Claims.(jwt.MapClaims)["email"].(string)
user := GetUser(ctx, email)
if user == nil {
w.WriteHeader(http.StatusNotFound)
return nil, cfg, nil, true
}
return db, cfg, user, false
}