add web ui and user route
This commit is contained in:
18
auth/jwt.go
18
auth/jwt.go
@@ -1,6 +1,7 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -28,3 +29,20 @@ func GenerateUserToken(userId int64, expiryTime time.Time) (string, error) {
|
||||
|
||||
return token.SignedString([]byte(secretKey))
|
||||
}
|
||||
|
||||
func ValidateUserToken(token string) (int64, error) {
|
||||
claims := &UserClaims{}
|
||||
|
||||
parsed, err := jwt.ParseWithClaims(token, claims, func(t *jwt.Token) (any, error) {
|
||||
return []byte(secretKey), nil
|
||||
})
|
||||
if err != nil {
|
||||
return -1, fmt.Errorf("failed to parse token: %v", err)
|
||||
}
|
||||
|
||||
if !parsed.Valid {
|
||||
return -1, fmt.Errorf("invalid token")
|
||||
}
|
||||
|
||||
return claims.UserID, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user