2023-12-27 17:31:49 +09:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2023-12-29 00:27:12 +09:00
|
|
|
"fmt"
|
2023-12-27 17:31:49 +09:00
|
|
|
"net/http"
|
2023-12-29 00:27:12 +09:00
|
|
|
"strings"
|
2023-12-27 17:31:49 +09:00
|
|
|
|
|
|
|
"learnsteam/cslms-api/internal/models"
|
|
|
|
"learnsteam/cslms-api/internal/services"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AuthController interface {
|
|
|
|
Register(*gin.Context)
|
|
|
|
Login(*gin.Context)
|
2023-12-29 00:27:12 +09:00
|
|
|
Logout(*gin.Context)
|
2023-12-27 17:31:49 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
type authController struct {
|
|
|
|
service services.AuthService
|
|
|
|
tokenService services.TokenService
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewAuthController(service services.AuthService, tokenService services.TokenService) AuthController {
|
|
|
|
return &authController{
|
|
|
|
service: service,
|
|
|
|
tokenService: tokenService,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Register
|
|
|
|
//
|
|
|
|
// @Summary 회원가입
|
2023-12-29 00:27:12 +09:00
|
|
|
// @Description 회원가입
|
2023-12-27 17:31:49 +09:00
|
|
|
// @Tags 회원가입
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
//
|
|
|
|
// @Param registerBody body models.RegisterRequest true "Register Body"
|
|
|
|
//
|
|
|
|
// @Success 200 {object} models.RegisterResponse
|
|
|
|
// @Router /auth/register [post]
|
|
|
|
func (controller *authController) Register(c *gin.Context) {
|
|
|
|
var params models.RegisterRequest
|
|
|
|
if c.BindJSON(¶ms) != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := controller.service.Register(¶ms)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := controller.tokenService.Create(user.ID, user.UserRole)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, gin.H{"user": user, "token": token.Token, "refresh_token": token.RefreshToken})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Login 사용자 로그인
|
|
|
|
//
|
|
|
|
// @Summary 사용자 로그인 로그인
|
2023-12-29 00:27:12 +09:00
|
|
|
// @Description 사용자가 로그인합니다.
|
2023-12-27 17:31:49 +09:00
|
|
|
// @Tags 로그인
|
|
|
|
//
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
//
|
|
|
|
// @Param loginBody body models.LoginRequest true "Login Body"
|
|
|
|
//
|
|
|
|
// @Success 200 {object} models.LoginResponse
|
|
|
|
// @Router /auth/login [post]
|
|
|
|
func (controller *authController) Login(c *gin.Context) {
|
|
|
|
var request models.LoginRequest
|
|
|
|
if err := c.BindJSON(&request); err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := controller.service.Login(&request)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
token, err := controller.tokenService.Create(user.ID, user.UserRole)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"user": user,
|
|
|
|
"token": token.Token,
|
|
|
|
"refresh_token": token.RefreshToken,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-29 00:27:12 +09:00
|
|
|
// Logout 로그아웃
|
|
|
|
//
|
|
|
|
// @Summary 로그아웃
|
|
|
|
// @Description 로그아웃하고 Token, Cookie 삭제
|
|
|
|
// @Tags 로그아웃
|
|
|
|
//
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
//
|
|
|
|
// @Security Bearer
|
|
|
|
//
|
|
|
|
// @Router /auth/logout [post]
|
2023-12-27 17:31:49 +09:00
|
|
|
func (controller *authController) Logout(c *gin.Context) {
|
2023-12-29 00:27:12 +09:00
|
|
|
token, err := func() (*string, error) {
|
|
|
|
authorization := c.GetHeader("Authorization")
|
|
|
|
strArr := strings.Split(authorization, " ")
|
|
|
|
if len(strArr) == 2 {
|
|
|
|
return &strArr[1], nil
|
|
|
|
} else {
|
|
|
|
return nil, fmt.Errorf("invalid authorization header")
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
2023-12-27 17:31:49 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-12-29 00:27:12 +09:00
|
|
|
user_id := c.GetInt64("sub")
|
|
|
|
|
|
|
|
err = controller.tokenService.Invalid(user_id, *token)
|
2023-12-27 17:31:49 +09:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2023-12-29 00:27:12 +09:00
|
|
|
|
2023-12-27 17:31:49 +09:00
|
|
|
c.SetCookie("Authorization", "", -1, "/", "localhost", false, true)
|
|
|
|
c.SetCookie("RefreshToken", "", -1, "/", "localhost", false, true)
|
|
|
|
c.SetCookie("ExpiresAt", "", -1, "/", "localhost", false, true)
|
|
|
|
c.SetCookie("RefreshExpiresAt", "", -1, "/", "localhost", false, true)
|
|
|
|
c.SetCookie("RefreshTokenExpiresAt", "", -1, "/", "localhost", false, true)
|
2023-12-29 00:27:12 +09:00
|
|
|
|
2023-12-27 17:31:49 +09:00
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "logout"})
|
|
|
|
}
|