From 654c1eb7b593ec89d265d576c641fb095c4e0f41 Mon Sep 17 00:00:00 2001 From: Nikolai Papin Date: Fri, 20 Jun 2025 13:33:06 +0300 Subject: [PATCH] feat: prototyping possible interaction of database, controllers, services with auth service --- backend/internal/controllers/auth.go | 19 +++++++++++++++ backend/internal/errors/auth.go | 3 +++ backend/internal/models/auth.go | 18 ++++++++++++++ backend/internal/services/auth.go | 35 +++++++++++++++++++++++++--- sqlc/query.sql | 2 +- 5 files changed, 73 insertions(+), 4 deletions(-) diff --git a/backend/internal/controllers/auth.go b/backend/internal/controllers/auth.go index fb11f20..941516c 100644 --- a/backend/internal/controllers/auth.go +++ b/backend/internal/controllers/auth.go @@ -14,6 +14,25 @@ import ( // @Produce json // @Router /auth/registrationBegin [post] 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) } diff --git a/backend/internal/errors/auth.go b/backend/internal/errors/auth.go index d7f6c90..a3d0959 100644 --- a/backend/internal/errors/auth.go +++ b/backend/internal/errors/auth.go @@ -6,4 +6,7 @@ import ( var ( 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") ) diff --git a/backend/internal/models/auth.go b/backend/internal/models/auth.go index 97fb8df..e9bdecc 100644 --- a/backend/internal/models/auth.go +++ b/backend/internal/models/auth.go @@ -5,6 +5,24 @@ type Tokens struct { 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 { Username string `json:"username"` Password string `json:"password"` diff --git a/backend/internal/services/auth.go b/backend/internal/services/auth.go index 1b9636c..b8d0f72 100644 --- a/backend/internal/services/auth.go +++ b/backend/internal/services/auth.go @@ -9,18 +9,48 @@ import ( ) type AuthService interface { + RegistrationBegin(request models.RegistrationBeginRequest) (bool, error) + RegistrationComplete(request models.RegistrationBeginRequest) (*models.RegistrationCompleteResponse, error) Login(request models.LoginRequest) (*models.LoginResponse, error) Refresh(request models.RefreshRequest) (*models.RefreshResponse, error) } type authServiceImpl struct { - } func NewAuthService() AuthService { 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) { conn, ctx, err := utils.GetDbConn() 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 } - func (a *authServiceImpl) Refresh(request models.RefreshRequest) (*models.RefreshResponse, error) { - return nil, errors.ErrNotImplemented + return nil, errors.ErrNotImplemented } diff --git a/sqlc/query.sql b/sqlc/query.sql index 87fb266..4ceb2e2 100644 --- a/sqlc/query.sql +++ b/sqlc/query.sql @@ -74,7 +74,7 @@ WHERE users.username = $1; --: Login Information Object {{{ -;-- name: CreateLoginInformation :one +;-- name: CreateLoginInformation :on INSERT INTO login_informations(user_id, email, password_hash) VALUES ( $1, $2, crypt(@password::text, gen_salt('bf')) ) RETURNING *;