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
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package admin
|
||||
import (
|
||||
"quyun/providers/ali"
|
||||
"quyun/providers/app"
|
||||
"quyun/providers/jwt"
|
||||
|
||||
"go.ipao.vip/atom"
|
||||
"go.ipao.vip/atom/container"
|
||||
@@ -11,6 +12,17 @@ import (
|
||||
)
|
||||
|
||||
func Provide(opts ...opt.Option) error {
|
||||
if err := container.Container.Provide(func(
|
||||
jwt *jwt.JWT,
|
||||
) (*auth, error) {
|
||||
obj := &auth{
|
||||
jwt: jwt,
|
||||
}
|
||||
|
||||
return obj, nil
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func() (*medias, error) {
|
||||
obj := &medias{}
|
||||
|
||||
@@ -33,6 +45,7 @@ func Provide(opts ...opt.Option) error {
|
||||
return err
|
||||
}
|
||||
if err := container.Container.Provide(func(
|
||||
auth *auth,
|
||||
medias *medias,
|
||||
orders *orders,
|
||||
posts *posts,
|
||||
@@ -40,6 +53,7 @@ func Provide(opts ...opt.Option) error {
|
||||
users *users,
|
||||
) (contracts.HttpRoute, error) {
|
||||
obj := &Routes{
|
||||
auth: auth,
|
||||
medias: medias,
|
||||
orders: orders,
|
||||
posts: posts,
|
||||
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
// @provider contracts.HttpRoute atom.GroupRoutes
|
||||
type Routes struct {
|
||||
log *log.Entry `inject:"false"`
|
||||
auth *auth
|
||||
medias *medias
|
||||
orders *orders
|
||||
posts *posts
|
||||
@@ -32,6 +33,12 @@ func (r *Routes) Name() string {
|
||||
}
|
||||
|
||||
func (r *Routes) Register(router fiber.Router) {
|
||||
// 注册路由组: auth
|
||||
router.Post("/v1/admin/auth", DataFunc1(
|
||||
r.auth.Login,
|
||||
Body[AuthBody]("body"),
|
||||
))
|
||||
|
||||
// 注册路由组: medias
|
||||
router.Get("/v1/admin/medias", DataFunc2(
|
||||
r.medias.List,
|
||||
|
||||
Reference in New Issue
Block a user