RyZ
feat: adding Role based auth, patch: patching csrf token with jwt and refresh token logic
2a792a5
raw
history blame contribute delete
868 Bytes
package utils
import (
"time"
"whatsapp-backend/config"
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
)
type JWTClaims struct {
UserID uuid.UUID `json:"user_id"`
Username string `json:"username"`
Role string `json:"role"`
jwt.RegisteredClaims
}
func GenerateAccessToken(userID uuid.UUID, username, role string, jwtConfig config.JWTConfig) (string, error) {
claims := JWTClaims{
UserID: userID,
Username: username,
Role: role,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(time.Now().Add(15 * time.Minute)), // 15 minutes expiration
IssuedAt: jwt.NewNumericDate(time.Now()),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(jwtConfig.GetSecretKey()))
}
func GenerateRefreshToken() (string, error) {
return uuid.New().String(), nil
}