feat: prototyping possible interaction of database, controllers, services with auth service
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
@@ -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")
|
||||
)
|
||||
|
||||
@@ -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"`
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 *;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user