feat: setup config (viper =)), jwt settings && session struct

This commit is contained in:
2025-09-06 12:58:42 +03:30
parent 9bade56794
commit c3c7e908f0
12 changed files with 202 additions and 5 deletions
+9
View File
@@ -0,0 +1,9 @@
package jwt
import jwt2 "github.com/golang-jwt/jwt/v5"
type UserClaims struct {
jwt2.RegisteredClaims
UserID uint
Sections []string
}
+37
View File
@@ -0,0 +1,37 @@
package jwt
import (
"errors"
jwt2 "github.com/golang-jwt/jwt/v5"
)
const UserClaimKey = "User-Claims"
func CreateToken(secret []byte, claims *UserClaims) (string, error) {
return jwt2.NewWithClaims(jwt2.SigningMethodHS512, claims).SignedString(secret)
}
func ParseToken(tokenString string, secret []byte) (*UserClaims, error) {
token, err := jwt2.ParseWithClaims(tokenString, &UserClaims{}, func(t *jwt2.Token) (interface{}, error) {
return secret, nil
})
var claim *UserClaims
if token.Claims != nil {
cc, ok := token.Claims.(*UserClaims)
if ok {
claim = cc
}
}
if err != nil {
return claim, err
}
if !token.Valid {
return claim, errors.New("token is not valid")
}
return claim, nil
}