2023-10-08 15:59:42 +07:00
|
|
|
package auth
|
|
|
|
|
|
|
|
|
|
import (
|
2024-07-23 01:36:25 +07:00
|
|
|
"fmt"
|
2023-10-08 15:59:42 +07:00
|
|
|
"furtuna-be/internal/constants/role"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
|
|
|
|
|
"furtuna-be/internal/common/errors"
|
|
|
|
|
auth2 "furtuna-be/internal/handlers/request"
|
|
|
|
|
"furtuna-be/internal/handlers/response"
|
|
|
|
|
"furtuna-be/internal/services"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type AuthHandler struct {
|
|
|
|
|
service services.Auth
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *AuthHandler) Route(group *gin.RouterGroup, jwt gin.HandlerFunc) {
|
|
|
|
|
authRoute := group.Group("/auth")
|
|
|
|
|
authRoute.POST("/login", a.AuthLogin)
|
2024-07-23 01:36:25 +07:00
|
|
|
authRoute.POST("/forgot-password", a.ForgotPassword)
|
|
|
|
|
authRoute.POST("/reset-password", jwt, a.ResetPassword)
|
2023-10-08 15:59:42 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewAuthHandler(service services.Auth) *AuthHandler {
|
|
|
|
|
return &AuthHandler{
|
|
|
|
|
service: service,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AuthLogin handles the authentication process for user login.
|
|
|
|
|
// @Summary User login
|
|
|
|
|
// @Description Authenticates a user based on the provided credentials and returns a JWT token.
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Param bodyParam body auth2.LoginRequest true "User login credentials"
|
|
|
|
|
// @Success 200 {object} response.BaseResponse{data=response.LoginResponse} "Login successful"
|
|
|
|
|
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
|
|
|
|
// @Failure 401 {object} response.BaseResponse{data=errors.Error} "Unauthorized"
|
|
|
|
|
// @Router /api/v1/auth/login [post]
|
|
|
|
|
// @Tags Auth Login API's
|
|
|
|
|
func (h *AuthHandler) AuthLogin(c *gin.Context) {
|
|
|
|
|
var bodyParam auth2.LoginRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&bodyParam); err != nil {
|
|
|
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
authUser, err := h.service.AuthenticateUser(c, bodyParam.Email, bodyParam.Password)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.ErrorWrapper(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-03 14:40:50 +07:00
|
|
|
var partner *response.Partner
|
2024-07-12 02:29:18 +07:00
|
|
|
var site *response.SiteName
|
2023-10-08 15:59:42 +07:00
|
|
|
|
|
|
|
|
if authUser.RoleID != role.SuperAdmin {
|
2024-06-03 14:40:50 +07:00
|
|
|
partner = &response.Partner{
|
|
|
|
|
ID: authUser.PartnerID,
|
|
|
|
|
Name: authUser.PartnerName,
|
|
|
|
|
Status: authUser.PartnerStatus,
|
2023-10-08 15:59:42 +07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-12 02:29:18 +07:00
|
|
|
if authUser.RoleID == role.Casheer {
|
|
|
|
|
site = &response.SiteName{
|
|
|
|
|
ID: authUser.SiteID,
|
|
|
|
|
Name: authUser.SiteName,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-08 15:59:42 +07:00
|
|
|
resp := response.LoginResponse{
|
2024-06-03 14:40:50 +07:00
|
|
|
Token: authUser.Token,
|
|
|
|
|
Partner: partner,
|
|
|
|
|
Name: authUser.Name,
|
2023-10-08 15:59:42 +07:00
|
|
|
Role: response.Role{
|
|
|
|
|
ID: int64(authUser.RoleID),
|
|
|
|
|
Role: authUser.RoleName,
|
|
|
|
|
},
|
2024-07-23 01:36:25 +07:00
|
|
|
Site: site,
|
|
|
|
|
ResetPassword: authUser.ResetPassword,
|
2023-10-08 15:59:42 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
|
|
|
Success: true,
|
|
|
|
|
Status: http.StatusOK,
|
|
|
|
|
Message: "Login Success",
|
|
|
|
|
Data: resp,
|
|
|
|
|
})
|
|
|
|
|
}
|
2024-07-23 01:36:25 +07:00
|
|
|
|
|
|
|
|
// ForgotPassword handles the request for password reset.
|
|
|
|
|
// @Summary Request password reset
|
|
|
|
|
// @Description Sends a password reset link to the user's email.
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Param bodyParam body auth2.ForgotPasswordRequest true "User email"
|
|
|
|
|
// @Success 200 {object} response.BaseResponse "Password reset link sent"
|
|
|
|
|
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
|
|
|
|
// @Router /api/v1/auth/forgot-password [post]
|
|
|
|
|
// @Tags Auth Password API's
|
|
|
|
|
func (h *AuthHandler) ForgotPassword(c *gin.Context) {
|
|
|
|
|
var bodyParam auth2.ResetPasswordRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&bodyParam); err != nil {
|
|
|
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := h.service.SendPasswordResetLink(c, bodyParam.Email)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.ErrorWrapper(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
|
|
|
Success: true,
|
|
|
|
|
Status: http.StatusOK,
|
|
|
|
|
Message: "Password reset link sent",
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ResetPassword handles the password reset process.
|
|
|
|
|
// @Summary Reset user password
|
|
|
|
|
// @Description Resets the user's password using the provided token.
|
|
|
|
|
// @Accept json
|
|
|
|
|
// @Produce json
|
|
|
|
|
// @Param bodyParam body auth2.ResetPasswordRequest true "Reset password details"
|
|
|
|
|
// @Success 200 {object} response.BaseResponse "Password reset successful"
|
|
|
|
|
// @Failure 400 {object} response.BaseResponse{data=errors.Error} "Bad request"
|
|
|
|
|
// @Router /api/v1/auth/reset-password [post]
|
|
|
|
|
// @Tags Auth Password API's
|
|
|
|
|
func (h *AuthHandler) ResetPassword(c *gin.Context) {
|
|
|
|
|
ctx := auth2.GetMyContext(c)
|
|
|
|
|
|
|
|
|
|
var req auth2.ResetPasswordChangeRequest
|
|
|
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
|
|
|
response.ErrorWrapper(c, errors.ErrorBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := req.Validate(); err != nil {
|
|
|
|
|
response.ErrorWrapper(c, errors.NewError(
|
|
|
|
|
errors.ErrorBadRequest.ErrorType(),
|
|
|
|
|
fmt.Sprintf("invalid request %v", err.Error())))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err := h.service.ResetPassword(ctx, req.OldPassword, req.NewPassword)
|
|
|
|
|
if err != nil {
|
|
|
|
|
response.ErrorWrapper(c, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, response.BaseResponse{
|
|
|
|
|
Success: true,
|
|
|
|
|
Status: http.StatusOK,
|
|
|
|
|
Message: "Password reset successful",
|
|
|
|
|
})
|
|
|
|
|
}
|