add membership

This commit is contained in:
Fran Jurmanović
2025-06-26 00:51:54 +02:00
parent 69733e4940
commit 74df36cd0d
24 changed files with 863 additions and 83 deletions

View File

@@ -17,10 +17,11 @@ import (
)
type RouteGroups struct {
Api fiber.Router
Server fiber.Router
Config fiber.Router
Lookup fiber.Router
Api fiber.Router
Auth fiber.Router
Server fiber.Router
Config fiber.Router
Lookup fiber.Router
StateHistory fiber.Router
}

View File

@@ -44,6 +44,9 @@ func Migrate(db *gorm.DB) {
&model.StateHistory{},
&model.SteamCredentials{},
&model.SystemConfig{},
&model.User{},
&model.Role{},
&model.Permission{},
)
if err != nil {

54
local/utl/jwt/jwt.go Normal file
View File

@@ -0,0 +1,54 @@
package jwt
import (
"acc-server-manager/local/model"
"errors"
"time"
"github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
)
// SecretKey is the secret key for signing the JWT.
// It is recommended to use a long, complex string for this.
// In a production environment, this should be loaded from a secure configuration source.
var SecretKey = []byte("your-secret-key")
// Claims represents the JWT claims.
type Claims struct {
UserID uuid.UUID `json:"user_id"`
jwt.RegisteredClaims
}
// GenerateToken generates a new JWT for a given user.
func GenerateToken(user *model.User) (string, error) {
expirationTime := time.Now().Add(24 * time.Hour)
claims := &Claims{
UserID: user.ID,
RegisteredClaims: jwt.RegisteredClaims{
ExpiresAt: jwt.NewNumericDate(expirationTime),
},
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(SecretKey)
}
// ValidateToken validates a JWT and returns the claims if the token is valid.
func ValidateToken(tokenString string) (*Claims, error) {
claims := &Claims{}
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return SecretKey, nil
})
if err != nil {
return nil, err
}
if !token.Valid {
return nil, errors.New("invalid token")
}
return claims, nil
}