Files
softplayer-backend-leg/internal/controllers/accounts.go
Nikolai Rodionov 874d762efa
Some checks failed
ci/woodpecker/push/build Pipeline failed
A couple of updates
Signed-off-by: Nikolai Rodionov <iam@allanger.xyz>
2026-04-28 10:33:20 +02:00

82 lines
1.9 KiB
Go

package controllers
import (
"context"
"database/sql"
"fmt"
"time"
"gitea.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/hash"
"github.com/golang-jwt/jwt/v5"
"github.com/google/uuid"
"github.com/redis/go-redis/v9"
)
type AccountController struct {
DB *sql.DB
Redis *redis.Client
DevMode bool
HashCost int16
AccessTokenTTL time.Duration
RefreshTokenTTL time.Duration
JWTSecret []byte
}
type JWT struct {
RefreshToken string
AccessToken string
}
type AccountParams struct{}
type AccountData struct {
Username string
Password string
Email string
UUID string
}
func (c *AccountController) Create(ctx context.Context, data *AccountData) (string, error) {
data.UUID = uuid.New().String()
passwordHash, err := hash.HashPassword(data.Password, int(c.HashCost))
if err != nil {
return "", nil
}
query := "INSERT INTO users (uuid, username, email, password_hash) VALUES ($1, $2, $3, $4)"
if _, err := c.DB.Query(query, data.UUID, data.Username, data.Email, passwordHash); err != nil {
return "", err
}
return data.UUID, nil
}
func (c *AccountController) GenerateAccessToken(userID string) (string, error) {
claims := jwt.MapClaims{
"user_id": userID,
"type": "access",
"exp": time.Now().Add(c.AccessTokenTTL).Unix(),
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(c.JWTSecret)
}
func (c *AccountController) GenerateRefreshToken(ctx context.Context, userID string) (string, error) {
tokenID := uuid.New().String()
claims := jwt.MapClaims{
"user_id": userID,
"token_id": tokenID,
"type": "refresh",
"exp": time.Now().Add(c.RefreshTokenTTL).Unix(),
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
redisKey := fmt.Sprintf("refresh_token:%s", tokenID)
if err := c.Redis.Set(ctx, redisKey, userID, c.RefreshTokenTTL).Err(); err != nil {
return "", err
}
return token.SignedString(c.JWTSecret)
}