47 lines
962 B
Go
47 lines
962 B
Go
package admin
|
|
|
|
import (
|
|
"quyun/v2/providers/jwt"
|
|
|
|
"github.com/gofiber/fiber/v3"
|
|
)
|
|
|
|
// @provider
|
|
type auth struct {
|
|
jwt *jwt.JWT
|
|
}
|
|
|
|
type AuthBody struct {
|
|
Username string `json:"username" validate:"required"`
|
|
Password string `json:"password" validate:"required"`
|
|
}
|
|
|
|
type TokenResponse struct {
|
|
Token string `json:"token"`
|
|
}
|
|
|
|
// Login
|
|
//
|
|
// @Summary 管理员登录
|
|
// @Tags Admin Auth
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Param body body AuthBody true "请求体"
|
|
// @Success 200 {object} TokenResponse "成功"
|
|
// @Router /admin/auth [post]
|
|
// @Bind body body
|
|
func (ctl *auth) Login(ctx fiber.Ctx, body *AuthBody) (*TokenResponse, error) {
|
|
if body.Username == "pl.yang" && body.Password == "Xixi@0202" {
|
|
claim := ctl.jwt.CreateClaims(jwt.BaseClaims{
|
|
UserID: -20140202,
|
|
})
|
|
|
|
token, err := ctl.jwt.CreateToken(claim)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &TokenResponse{Token: token}, nil
|
|
}
|
|
return nil, fiber.ErrUnauthorized
|
|
}
|