41 lines
850 B
Go
41 lines
850 B
Go
package controllers
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"gitea.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/hash"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type AccountController struct {
|
|
Params AccountParams
|
|
DB *sql.DB
|
|
DevMode bool
|
|
}
|
|
|
|
type AccountParams struct {
|
|
HashCost int16
|
|
}
|
|
|
|
type AccountData struct {
|
|
Username string
|
|
Password string
|
|
Email string
|
|
UUID string
|
|
}
|
|
|
|
func (c *AccountController) Create(ctx context.Context, data *AccountData) error {
|
|
data.UUID = uuid.New().String()
|
|
|
|
passwordHash, err := hash.HashPassword(data.Password, int(c.Params.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 nil
|
|
}
|