feat: add admin login
This commit is contained in:
35
backend/app/http/admin/auth.go
Normal file
35
backend/app/http/admin/auth.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"quyun/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"`
|
||||
}
|
||||
|
||||
// Login
|
||||
// @Router /v1/admin/auth [post]
|
||||
// @Bind body body
|
||||
func (ctl *auth) Login(ctx fiber.Ctx, body *AuthBody) (string, error) {
|
||||
if body.Username == "admin" && body.Password == "xixi@0202" {
|
||||
claim := ctl.jwt.CreateClaims(jwt.BaseClaims{
|
||||
UserID: 1,
|
||||
})
|
||||
|
||||
token, err := ctl.jwt.CreateToken(claim)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return token, nil
|
||||
}
|
||||
return "", fiber.ErrUnauthorized
|
||||
}
|
||||
Reference in New Issue
Block a user