123 lines
2.9 KiB
Go
123 lines
2.9 KiB
Go
package api
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"net/http"
|
|
|
|
"git.heldm.com/held/qrcode/internal/app/dto"
|
|
"git.heldm.com/held/qrcode/internal/app/handler"
|
|
"git.heldm.com/held/qrcode/internal/app/repository"
|
|
"git.heldm.com/held/qrcode/internal/app/service/response"
|
|
"git.heldm.com/held/qrcode/internal/app/usecase/auth"
|
|
"git.heldm.com/held/qrcode/internal/qrcode/entity"
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AuthHandler struct{}
|
|
|
|
func (h AuthHandler) Init(r *gin.RouterGroup) {
|
|
r.POST("/signup", h.signUp)
|
|
r.POST("/login", h.logIn)
|
|
}
|
|
|
|
func (h AuthHandler) signUp(c *gin.Context) {
|
|
|
|
var signUpRequest dto.UserSignUpRequest
|
|
if err := c.ShouldBindJSON(&signUpRequest); err != nil {
|
|
response.JSON(response.Response{
|
|
Status: http.StatusBadRequest,
|
|
Msg: handler.ErrInvalidRequest,
|
|
Data: err.Error(),
|
|
}, c)
|
|
return
|
|
}
|
|
|
|
newUser, err := entity.NewUser(signUpRequest.Name, signUpRequest.Email, signUpRequest.Password)
|
|
if err != nil {
|
|
response.JSON(response.Response{
|
|
Status: http.StatusBadRequest,
|
|
Msg: handler.ErrInvalidRequest,
|
|
Data: err.Error(),
|
|
}, c)
|
|
return
|
|
}
|
|
|
|
if err := auth.SignUp(c.Request.Context(), newUser); err != nil {
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, repository.ErrEmailAlredyExists):
|
|
response.JSON(response.Response{
|
|
Status: http.StatusConflict,
|
|
Msg: repository.ErrEmailAlredyExists.Error(),
|
|
Data: err.Error(),
|
|
}, c)
|
|
default:
|
|
response.JSON(response.Response{
|
|
Status: http.StatusInternalServerError,
|
|
Msg: handler.ErrInternalError,
|
|
Data: err.Error(),
|
|
}, c)
|
|
}
|
|
return
|
|
}
|
|
}
|
|
|
|
token, err := auth.GenerateToken(newUser)
|
|
if err != nil {
|
|
response.JSON(response.Response{
|
|
Status: http.StatusInternalServerError,
|
|
Msg: fmt.Sprintf("%s. Failed to generate token", handler.ErrInternalError),
|
|
Data: err.Error(),
|
|
}, c)
|
|
return
|
|
}
|
|
|
|
auth.CreateCookie(c, token)
|
|
|
|
response.JSON(response.Response{
|
|
Status: http.StatusCreated,
|
|
Msg: "User created successfully",
|
|
Data: nil,
|
|
}, c)
|
|
}
|
|
|
|
func (h AuthHandler) logIn(c *gin.Context) {
|
|
var logInRequest dto.UserLogInRequest
|
|
if err := c.ShouldBindJSON(&logInRequest); err != nil {
|
|
response.JSON(response.Response{
|
|
Status: http.StatusBadRequest,
|
|
Msg: handler.ErrInvalidRequest,
|
|
Data: err.Error(),
|
|
}, c)
|
|
return
|
|
}
|
|
|
|
token, err := auth.LogIn(c.Request.Context(), &logInRequest)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, auth.ErrInvalidCredentials):
|
|
response.JSON(response.Response{
|
|
Status: http.StatusUnauthorized,
|
|
Msg: fmt.Sprintf("%s. Invalid email or password", handler.ErrInvalidRequest),
|
|
Data: err.Error(),
|
|
}, c)
|
|
default:
|
|
response.JSON(response.Response{
|
|
Status: http.StatusInternalServerError,
|
|
Msg: fmt.Sprintf("%s. Failed to log in", handler.ErrInternalError),
|
|
Data: err.Error(),
|
|
}, c)
|
|
}
|
|
return
|
|
}
|
|
|
|
auth.CreateCookie(c, token)
|
|
|
|
response.JSON(response.Response{
|
|
Status: http.StatusOK,
|
|
Msg: "Log in successful",
|
|
Data: nil,
|
|
}, c)
|
|
}
|