2022-10-06 16:41:32 +08:00
|
|
|
package auth
|
2022-10-06 17:05:15 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gofiber/fiber/v2"
|
2022-10-06 17:23:22 +08:00
|
|
|
"gofiber.study.skcks.cn/common/errorx"
|
|
|
|
"gofiber.study.skcks.cn/common/response"
|
2022-10-06 17:05:15 +08:00
|
|
|
"gofiber.study.skcks.cn/controller/types"
|
2022-10-06 20:46:17 +08:00
|
|
|
"gofiber.study.skcks.cn/model/dto"
|
2022-10-06 17:58:11 +08:00
|
|
|
"gofiber.study.skcks.cn/services/auth"
|
2022-10-06 17:05:15 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type Controller struct {
|
|
|
|
*types.Controller
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Controller) GetRouter() fiber.Router {
|
|
|
|
return c.Router
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewController(app *fiber.App) *Controller {
|
|
|
|
return &Controller{
|
|
|
|
Controller: types.NewController(app, "/auth"),
|
|
|
|
}
|
|
|
|
}
|
2022-10-06 17:23:22 +08:00
|
|
|
|
|
|
|
// Login 用户登录
|
|
|
|
//
|
|
|
|
// @Summary 用户登录
|
|
|
|
// @Description 用户登录
|
|
|
|
// @Tags Auth
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param vo body dto.Login true "用户登录"
|
2022-10-06 22:12:43 +08:00
|
|
|
// @Success 200 {object} response.Response{data=vo.Login}
|
2022-10-06 17:23:22 +08:00
|
|
|
// @Failure default {object} errorx.CodeErrorResponse
|
|
|
|
// @Router /auth/login [post]
|
|
|
|
func (c *Controller) Login() {
|
|
|
|
c.Router.Post("login", func(ctx *fiber.Ctx) error {
|
|
|
|
login := &dto.Login{}
|
|
|
|
err := ctx.BodyParser(login)
|
|
|
|
if err = errorx.ParseError(err); err != nil {
|
|
|
|
return ctx.JSON(err)
|
|
|
|
}
|
|
|
|
|
2022-10-06 22:12:43 +08:00
|
|
|
result, err := auth.Services.Login(login)
|
2022-10-06 17:23:22 +08:00
|
|
|
if err = errorx.ParseError(err); err != nil {
|
|
|
|
return ctx.JSON(err)
|
|
|
|
}
|
|
|
|
|
2022-10-06 22:12:43 +08:00
|
|
|
return ctx.JSON(response.NewResponse(result))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RefreshToken 刷新令牌
|
|
|
|
//
|
|
|
|
// @Summary 刷新令牌
|
|
|
|
// @Description 刷新令牌
|
|
|
|
// @Tags Auth
|
|
|
|
// @Accept json
|
|
|
|
// @Produce json
|
|
|
|
// @Param vo body dto.RefreshToken true "刷新令牌"
|
|
|
|
// @Success 200 {object} response.Response{data=vo.Login}
|
|
|
|
// @Failure default {object} errorx.CodeErrorResponse
|
|
|
|
// @Router /auth/refreshToken [post]
|
|
|
|
func (c *Controller) RefreshToken() {
|
|
|
|
c.Router.Post("refreshToken", func(ctx *fiber.Ctx) error {
|
|
|
|
refresh := &dto.RefreshToken{}
|
|
|
|
err := ctx.BodyParser(refresh)
|
2022-10-06 22:22:54 +08:00
|
|
|
if err = errorx.ParseErrorWithCode(fiber.StatusUnauthorized, err); err != nil {
|
2022-10-06 22:12:43 +08:00
|
|
|
return ctx.JSON(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result, err := auth.Services.RefreshToken(refresh.RefreshToken)
|
2022-10-06 22:22:54 +08:00
|
|
|
if err = errorx.ParseErrorWithCode(fiber.StatusUnauthorized, err); err != nil {
|
2022-10-06 22:12:43 +08:00
|
|
|
return ctx.JSON(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.JSON(response.NewResponse(result))
|
2022-10-06 17:23:22 +08:00
|
|
|
})
|
|
|
|
}
|