feat: prototyping possible interaction of database, controllers, services with auth service

This commit is contained in:
2025-06-20 13:33:06 +03:00
parent 87878f15a3
commit 654c1eb7b5
5 changed files with 73 additions and 4 deletions

View File

@@ -14,6 +14,25 @@ import (
// @Produce json // @Produce json
// @Router /auth/registrationBegin [post] // @Router /auth/registrationBegin [post]
func RegistrationBegin(c *gin.Context) { func RegistrationBegin(c *gin.Context) {
var request models.RegistrationBeginRequest
if err := c.ShouldBindJSON(&request); err != nil {
c.Status(http.StatusBadRequest)
return
}
auths := services.NewAuthService()
result, err := auths.Login(request)
if err != nil {
c.Status(http.StatusTeapot)
return
}
c.JSON(http.StatusFound, result)
c.Status(http.StatusNotImplemented) c.Status(http.StatusNotImplemented)
} }

View File

@@ -6,4 +6,7 @@ import (
var ( var (
ErrUnauthorized = errors.New("User is not authorized") ErrUnauthorized = errors.New("User is not authorized")
ErrUsernameTaken = errors.New("Provided username is already in use")
ErrInvalidCredentials = errors.New("Invalid username, password or TOTP code")
ErrInvalidToken = errors.New("Token is invalid or expired")
) )

View File

@@ -5,6 +5,24 @@ type Tokens struct {
RefreshToken string `json:"refresh_token"` RefreshToken string `json:"refresh_token"`
} }
type RegistrationBeginRequest struct {
Username string `json:"username"`
Email *string `json:"email"`
Password string `json:"password"` // TODO: password checking
}
type RegistrationCompleteRequest struct {
Username string `json:"username"`
VerificationCode string `json:"verification_code"`
Name string `json:"name"`
Birthday *string `json:"birthday"`
AvatarUrl *string `json:"avatar_url"`
}
type RegistrationCompleteResponse struct {
Tokens
}
type LoginRequest struct { type LoginRequest struct {
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"` Password string `json:"password"`

View File

@@ -9,18 +9,48 @@ import (
) )
type AuthService interface { type AuthService interface {
RegistrationBegin(request models.RegistrationBeginRequest) (bool, error)
RegistrationComplete(request models.RegistrationBeginRequest) (*models.RegistrationCompleteResponse, error)
Login(request models.LoginRequest) (*models.LoginResponse, error) Login(request models.LoginRequest) (*models.LoginResponse, error)
Refresh(request models.RefreshRequest) (*models.RefreshResponse, error) Refresh(request models.RefreshRequest) (*models.RefreshResponse, error)
} }
type authServiceImpl struct { type authServiceImpl struct {
} }
func NewAuthService() AuthService { func NewAuthService() AuthService {
return &authServiceImpl{} return &authServiceImpl{}
} }
func (a *authServiceImpl) RegistrationBegin(request models.RegistrationBeginRequest) (bool, error) {
conn, ctx, err := utils.GetDbConn()
if err != nil {
return false, err
}
defer conn.Close(ctx)
queries := database.New(conn)
tx, err := database.Begin()
if err != nil {
return err
}
defer tx.Rollback()
qtx := queries.WithTx(tx)
tx.Commit()
return
}
func (a *authServiceImpl) RegistrationComplete(request models.RegistrationBeginRequest) (*models.RegistrationCompleteResponse, error) {
conn, ctx, err := utils.GetDbConn()
if err != nil {
return nil, err
}
defer conn.Close(ctx)
}
func (a *authServiceImpl) Login(request models.LoginRequest) (*models.LoginResponse, error) { func (a *authServiceImpl) Login(request models.LoginRequest) (*models.LoginResponse, error) {
conn, ctx, err := utils.GetDbConn() conn, ctx, err := utils.GetDbConn()
if err != nil { if err != nil {
@@ -44,7 +74,6 @@ func (a *authServiceImpl) Login(request models.LoginRequest) (*models.LoginRespo
return &models.LoginResponse{Tokens: models.Tokens{AccessToken: accessToken, RefreshToken: refreshToken}}, nil return &models.LoginResponse{Tokens: models.Tokens{AccessToken: accessToken, RefreshToken: refreshToken}}, nil
} }
func (a *authServiceImpl) Refresh(request models.RefreshRequest) (*models.RefreshResponse, error) { func (a *authServiceImpl) Refresh(request models.RefreshRequest) (*models.RefreshResponse, error) {
return nil, errors.ErrNotImplemented return nil, errors.ErrNotImplemented
} }

View File

@@ -74,7 +74,7 @@ WHERE users.username = $1;
--: Login Information Object {{{ --: Login Information Object {{{
;-- name: CreateLoginInformation :one ;-- name: CreateLoginInformation :on
INSERT INTO login_informations(user_id, email, password_hash) INSERT INTO login_informations(user_id, email, password_hash)
VALUES ( $1, $2, crypt(@password::text, gen_salt('bf')) ) RETURNING *; VALUES ( $1, $2, crypt(@password::text, gen_salt('bf')) ) RETURNING *;