This commit is contained in:
40
backend_v1/app/http/admin/auth.go
Normal file
40
backend_v1/app/http/admin/auth.go
Normal file
@@ -0,0 +1,40 @@
|
||||
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
|
||||
//
|
||||
// @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
|
||||
}
|
||||
Reference in New Issue
Block a user