cslms-api/internal/controllers/auth.go
2023-12-29 00:27:12 +09:00

148 lines
3.7 KiB
Go

package controllers
import (
"fmt"
"net/http"
"strings"
"learnsteam/cslms-api/internal/models"
"learnsteam/cslms-api/internal/services"
"github.com/gin-gonic/gin"
)
type AuthController interface {
Register(*gin.Context)
Login(*gin.Context)
Logout(*gin.Context)
}
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 회원가입
// @Description 회원가입
// @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(&params) != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"})
return
}
user, err := controller.service.Register(&params)
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 사용자 로그인 로그인
// @Description 사용자가 로그인합니다.
// @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,
})
}
// Logout 로그아웃
//
// @Summary 로그아웃
// @Description 로그아웃하고 Token, Cookie 삭제
// @Tags 로그아웃
//
// @Accept json
// @Produce json
//
// @Security Bearer
//
// @Router /auth/logout [post]
func (controller *authController) Logout(c *gin.Context) {
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()})
return
}
user_id := c.GetInt64("sub")
err = controller.tokenService.Invalid(user_id, *token)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
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)
c.JSON(http.StatusOK, gin.H{"message": "logout"})
}