2023-10-19 22:47:12 +09:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2023-11-17 01:38:01 +09:00
|
|
|
"learnsteam/learsteam-quiz-api/internal/models"
|
|
|
|
"learnsteam/learsteam-quiz-api/internal/services"
|
2023-10-19 22:47:12 +09:00
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type AuthController interface {
|
|
|
|
Register(*gin.Context)
|
|
|
|
Login(*gin.Context)
|
|
|
|
}
|
|
|
|
|
|
|
|
type authController struct {
|
2023-11-17 01:38:01 +09:00
|
|
|
service services.AuthService
|
|
|
|
tokenService services.TokenService
|
2023-10-19 22:47:12 +09:00
|
|
|
}
|
|
|
|
|
2023-11-17 01:38:01 +09:00
|
|
|
func NewAuthController(service services.AuthService, tokenService services.TokenService) AuthController {
|
2023-10-19 22:47:12 +09:00
|
|
|
return &authController{
|
2023-11-17 01:38:01 +09:00
|
|
|
service: service,
|
|
|
|
tokenService: tokenService,
|
2023-10-19 22:47:12 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-17 01:38:01 +09:00
|
|
|
// Register
|
|
|
|
//
|
|
|
|
// @Summary 회원가입
|
|
|
|
// @Description username, name, password 를 입력하여 회원가입
|
2023-11-17 14:27:42 +09:00
|
|
|
// @Tags 회원가입
|
|
|
|
// @Accept json
|
2023-11-17 01:38:01 +09:00
|
|
|
// @Produce json
|
|
|
|
//
|
|
|
|
// @Param username body string true "username"
|
|
|
|
// @Param name body string true "이름"
|
|
|
|
// @Param password body string true "비밀번호"
|
|
|
|
//
|
|
|
|
// @Success 200 {object} models.RegisterResponse
|
|
|
|
// @Router /auth/register [post]
|
2023-10-19 22:47:12 +09:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-11-17 01:38:01 +09:00
|
|
|
token, err := controller.tokenService.Create(user.ID)
|
2023-10-19 22:47:12 +09:00
|
|
|
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-11-18 13:03:49 +09:00
|
|
|
// Login 사용자 로그인
|
2023-11-17 01:38:01 +09:00
|
|
|
//
|
2023-11-18 13:03:49 +09:00
|
|
|
// @Summary 사용자 로그인 로그인
|
|
|
|
// @Description 사용자가 로그인합니다.
|
2023-11-17 14:27:42 +09:00
|
|
|
// @Tags 로그인
|
2023-11-17 01:38:01 +09:00
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
//
|
|
|
|
// @Param username body string true "username"
|
|
|
|
// @Param password body string true "비밀번호"
|
|
|
|
//
|
|
|
|
// @Success 200 {object} models.LoginResponse
|
|
|
|
// @Router /auth/login [post]
|
2023-10-19 22:47:12 +09:00
|
|
|
func (controller *authController) Login(c *gin.Context) {
|
2023-11-17 01:38:01 +09:00
|
|
|
var request models.LoginRequest
|
2023-11-18 13:03:49 +09:00
|
|
|
if err := c.BindJSON(&request); err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
2023-10-19 22:47:12 +09:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-17 01:38:01 +09:00
|
|
|
user, err := controller.service.Login(&request)
|
2023-10-19 22:47:12 +09:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-17 01:38:01 +09:00
|
|
|
token, err := controller.tokenService.Create(user.ID)
|
2023-10-19 22:47:12 +09:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2023-11-17 01:38:01 +09:00
|
|
|
|
2023-11-18 13:03:49 +09:00
|
|
|
c.JSON(http.StatusOK, gin.H{
|
|
|
|
"user": user,
|
|
|
|
"token": token.Token,
|
|
|
|
"refresh_token": token.RefreshToken,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Logout
|
|
|
|
func (controller *authController) Logout(c *gin.Context) {
|
|
|
|
token := c.GetHeader("Authorization")
|
|
|
|
if token == "" {
|
|
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "bad request"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := controller.tokenService.Delete(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.SetCookie("RefreshTokenExpiresAt", "", -1, "/", "localhost", false, true)
|
|
|
|
c.SetCookie("RefreshTokenExpiresAt", "", -1, "/", "localhost", false, true)
|
|
|
|
c.JSON(http.StatusOK, gin.H{"message": "logout"})
|
2023-10-19 22:47:12 +09:00
|
|
|
}
|