11package handlers
22
33import (
4+ "api-shiners/api/handlers/dto"
45 "api-shiners/pkg/auth"
56 "api-shiners/pkg/utils"
67 "context"
@@ -10,7 +11,6 @@ import (
1011 "github.com/gofiber/fiber/v2"
1112)
1213
13- // AuthController handles authentication related endpoints
1414type AuthController struct {
1515 authService auth.AuthService
1616}
@@ -19,17 +19,16 @@ func NewAuthController(authService auth.AuthService) AuthController {
1919 return AuthController {authService : authService }
2020}
2121
22- // ==================== REGISTER ====================
2322
24- // Register godoc
2523// @Summary Register a new user
26- // @Description Create a new user account
24+ // @Description Membuat akun user baru
2725// @Tags Auth
2826// @Accept json
2927// @Produce json
30- // @Param request body auth.RegisterRequest true "Register Request"
31- // @Success 201 {object} map[string]interface{}
32- // @Failure 400 {object} map[string]interface{}
28+ // @Param request body dto.RegisterRequest true "Register Request"
29+ // @Success 201 {object} dto.RegisterResponse
30+ // @Failure 400 {object} utils.ErrorResponse
31+ // @Failure 500 {object} utils.ErrorResponse
3332// @Router /api/auth/register [post]
3433func (ctrl * AuthController ) Register (c * fiber.Ctx ) error {
3534 var req auth.RegisterRequest
@@ -45,49 +44,53 @@ func (ctrl *AuthController) Register(c *fiber.Ctx) error {
4544 return utils .Success (c , http .StatusCreated , "User registered successfully" , createdUser , nil )
4645}
4746
48- // ==================== LOGIN ====================
4947
50- // Login godoc
5148// @Summary Login user
52- // @Description Authenticate user and return JWT token
49+ // @Description Autentikasi user dan mendapatkan JWT token
5350// @Tags Auth
5451// @Accept json
5552// @Produce json
56- // @Param request body auth.LoginRequest true "Login Request"
57- // @Success 200 {object} map[string]interface{}
58- // @Failure 401 {object} map[string]interface{}
53+ // @Param request body dto.LoginRequest true "Login Request"
54+ // @Success 200 {object} dto.LoginResponse
55+ // @Failure 400 {object} utils.ErrorResponse
56+ // @Failure 401 {object} utils.ErrorResponse
5957// @Router /api/auth/login [post]
6058func (ctrl * AuthController ) Login (c * fiber.Ctx ) error {
6159 var req auth.LoginRequest
6260 if err := c .BodyParser (& req ); err != nil {
6361 return utils .Error (c , http .StatusBadRequest , "Invalid request body" , "BadRequestException" , nil )
6462 }
6563
66- token , exp , err := ctrl .authService .Login (context .Background (), req )
64+ user , token , exp , permissions , err := ctrl .authService .LoginCore (context .Background (), req )
6765 if err != nil {
6866 return utils .Error (c , http .StatusUnauthorized , err .Error (), "UnauthorizedException" , nil )
6967 }
7068
7169 data := fiber.Map {
72- "token" : token ,
73- "expires_in" : exp .Format (time .RFC3339 ),
74- "token_type" : "Bearer" ,
70+ "token" : token ,
71+ "expires_in" : exp .Format (time .RFC3339 ),
72+ "token_type" : "Bearer" ,
73+ "user" : fiber.Map {
74+ "id" : user .ID ,
75+ "name" : user .Name ,
76+ "role" : user .Roles ,
77+ "permissions" : permissions ,
78+ },
7579 }
7680
7781 return utils .Success (c , http .StatusOK , "Login successful" , data , nil )
7882}
7983
80- // ==================== LOGOUT ====================
8184
82- // Logout godoc
8385// @Summary Logout user
84- // @Description Invalidate user token
86+ // @Description Mengakhiri sesi dan menonaktifkan token
8587// @Tags Auth
8688// @Accept json
8789// @Produce json
8890// @Security BearerAuth
89- // @Success 200 {object} map[string]interface{}
90- // @Failure 400 {object} map[string]interface{}
91+ // @Success 200 {object} dto.GenericResponse
92+ // @Failure 400 {object} utils.ErrorResponse
93+ // @Failure 401 {object} utils.ErrorResponse
9194// @Router /api/auth/logout [post]
9295func (ctrl * AuthController ) Logout (c * fiber.Ctx ) error {
9396 token := c .Get ("Authorization" )
@@ -107,22 +110,19 @@ func (ctrl *AuthController) Logout(c *fiber.Ctx) error {
107110 return utils .Success (c , http .StatusOK , "Logout successful" , nil , nil )
108111}
109112
110- // ==================== FORGOT PASSWORD ====================
111113
112- // ForgotPassword godoc
113114// @Summary Request password reset
114- // @Description Generate reset token and send it to user's email
115+ // @Description Generate reset token dan kirim ke email user
115116// @Tags Auth
116117// @Accept json
117118// @Produce json
118- // @Param request body map[string]string true "Email Request"
119- // @Success 200 {object} map[string]interface{}
120- // @Failure 400 {object} map[string]interface{}
119+ // @Param request body dto.ForgotPasswordRequest true "Forgot Password Request"
120+ // @Success 200 {object} dto.GenericResponse
121+ // @Failure 400 {object} utils.ErrorResponse
122+ // @Failure 500 {object} utils.ErrorResponse
121123// @Router /api/auth/forgot-password [post]
122124func (ctrl * AuthController ) ForgotPassword (c * fiber.Ctx ) error {
123- var req struct {
124- Email string `json:"email"`
125- }
125+ var req dto.ForgotPasswordRequest
126126 if err := c .BodyParser (& req ); err != nil || req .Email == "" {
127127 return utils .Error (c , http .StatusBadRequest , "Email is required" , "BadRequestException" , nil )
128128 }
@@ -134,27 +134,23 @@ func (ctrl *AuthController) ForgotPassword(c *fiber.Ctx) error {
134134
135135 return utils .Success (c , http .StatusOK , "Password reset token generated" , fiber.Map {
136136 "email" : req .Email ,
137- "token" : token , // tampilkan untuk testing
137+ "token" : token , // ditampilkan untuk keperluan testing
138138 }, nil )
139139}
140140
141- // ==================== RESET PASSWORD ====================
142141
143- // ResetPassword godoc
144142// @Summary Reset user password
145- // @Description Reset password using valid reset token
143+ // @Description Reset password menggunakan reset token yang valid
146144// @Tags Auth
147145// @Accept json
148146// @Produce json
149- // @Param request body map[string]string true "Reset Password Request"
150- // @Success 200 {object} map[string]interface{}
151- // @Failure 400 {object} map[string]interface{}
147+ // @Param request body dto.ResetPasswordRequest true "Reset Password Request"
148+ // @Success 200 {object} dto.GenericResponse
149+ // @Failure 400 {object} utils.ErrorResponse
150+ // @Failure 500 {object} utils.ErrorResponse
152151// @Router /api/auth/reset-password [post]
153152func (ctrl * AuthController ) ResetPassword (c * fiber.Ctx ) error {
154- var req struct {
155- Token string `json:"token"`
156- NewPassword string `json:"new_password"`
157- }
153+ var req dto.ResetPasswordRequest
158154 if err := c .BodyParser (& req ); err != nil || req .Token == "" || req .NewPassword == "" {
159155 return utils .Error (c , http .StatusBadRequest , "Token and new password required" , "BadRequestException" , nil )
160156 }
0 commit comments