136 lines
3.4 KiB
Go
136 lines
3.4 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"users_management/m/model/dto"
|
|
"users_management/m/usecase"
|
|
"users_management/m/utils/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type AuthController struct {
|
|
authUC usecase.AuthUsecase
|
|
rg *gin.RouterGroup
|
|
}
|
|
|
|
func NewAuthController(authUC usecase.AuthUsecase, rg *gin.RouterGroup) *AuthController {
|
|
return &AuthController{
|
|
authUC: authUC,
|
|
rg: rg,
|
|
}
|
|
}
|
|
|
|
func (c *AuthController) Route() {
|
|
auth := c.rg.Group("/auth")
|
|
{
|
|
auth.POST("/login", c.login)
|
|
auth.POST("/logout", c.logout)
|
|
auth.POST("/validate", c.validateToken)
|
|
auth.POST("/test-token-expiry", c.testTokenExpiry)
|
|
}
|
|
}
|
|
|
|
func (c *AuthController) login(ctx *gin.Context) {
|
|
var loginDTO dto.UserLoginDTO
|
|
if err := ctx.ShouldBindJSON(&loginDTO); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
token, role, name, err := c.authUC.Login(loginDTO)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusUnauthorized, err.Error())
|
|
return
|
|
}
|
|
|
|
response := gin.H{
|
|
"token": token,
|
|
"user": gin.H{
|
|
"name": name,
|
|
"username": loginDTO.Username,
|
|
"role": role,
|
|
},
|
|
"expires_in": "24h",
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Login successful", response)
|
|
}
|
|
|
|
func (c *AuthController) logout(ctx *gin.Context) {
|
|
token := ctx.GetHeader("Authorization")
|
|
if token == "" {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Authorization token required")
|
|
return
|
|
}
|
|
|
|
// Remove "Bearer " prefix
|
|
if len(token) > 7 && token[:7] == "Bearer " {
|
|
token = token[7:]
|
|
}
|
|
|
|
err := c.authUC.Logout(token)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Logout successful", nil)
|
|
}
|
|
|
|
func (c *AuthController) validateToken(ctx *gin.Context) {
|
|
token := ctx.GetHeader("Authorization")
|
|
if token == "" {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Authorization token required")
|
|
return
|
|
}
|
|
|
|
// Remove "Bearer " prefix
|
|
if len(token) > 7 && token[:7] == "Bearer " {
|
|
token = token[7:]
|
|
}
|
|
|
|
username, err := c.authUC.ValidateToken(token)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusUnauthorized, err.Error())
|
|
return
|
|
}
|
|
|
|
response := gin.H{
|
|
"valid": true,
|
|
"username": username,
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Token is valid", response)
|
|
}
|
|
|
|
func (c *AuthController) testTokenExpiry(ctx *gin.Context) {
|
|
token := ctx.GetHeader("Authorization")
|
|
if token == "" {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Authorization token required")
|
|
return
|
|
}
|
|
|
|
// Remove "Bearer " prefix
|
|
if len(token) > 7 && token[:7] == "Bearer " {
|
|
token = token[7:]
|
|
}
|
|
|
|
// Test if token is expired
|
|
_, err := c.authUC.ValidateToken(token)
|
|
if err != nil {
|
|
response := gin.H{
|
|
"expired": true,
|
|
"message": err.Error(),
|
|
}
|
|
common.SingleResponses(ctx, "Token expiry test result", response)
|
|
return
|
|
}
|
|
|
|
response := gin.H{
|
|
"expired": false,
|
|
"message": "Token is still valid",
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Token expiry test result", response)
|
|
} |