feat: add tenant commands
This commit is contained in:
@@ -1,15 +1,13 @@
|
||||
package middlewares
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"backend/common/consts"
|
||||
|
||||
"github.com/gofiber/fiber/v3"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func (f *Middlewares) JwtParse(c fiber.Ctx) error {
|
||||
func (f *Middlewares) ParseJWT(c fiber.Ctx) error {
|
||||
tokens := c.GetReqHeaders()["Authorization"]
|
||||
if len(tokens) == 0 {
|
||||
return c.Next()
|
||||
@@ -22,13 +20,20 @@ func (f *Middlewares) JwtParse(c fiber.Ctx) error {
|
||||
}
|
||||
|
||||
// query user
|
||||
user, err := f.userSvc.GetByOpenID(c.Context(), claim.ID)
|
||||
user, err := f.userSvc.GetByOpenID(c.Context(), claim.OpenID)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get user")
|
||||
}
|
||||
claim.UserID = user.ID
|
||||
|
||||
c.SetUserContext(context.WithValue(c.UserContext(), consts.CtxKeyJwt, token))
|
||||
c.SetUserContext(context.WithValue(c.UserContext(), consts.CtxKeySession, user))
|
||||
tenantId, err := f.userSvc.GetTenantIDBySlug(c.Context(), claim.Tenant)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get tenant")
|
||||
}
|
||||
claim.TenantID = tenantId
|
||||
|
||||
c.Locals(consts.CtxKeyJwt, token)
|
||||
c.Locals(consts.CtxKeyClaim, claim)
|
||||
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
@@ -11,6 +11,10 @@ import (
|
||||
)
|
||||
|
||||
func (f *Middlewares) WeChatAuthUserInfo(c fiber.Ctx) error {
|
||||
if len(c.GetReqHeaders()["Authorization"]) != 0 {
|
||||
return c.Next()
|
||||
}
|
||||
|
||||
state := c.Query("state")
|
||||
code := c.Query("code")
|
||||
|
||||
@@ -40,13 +44,16 @@ func (f *Middlewares) WeChatAuthUserInfo(c fiber.Ctx) error {
|
||||
}
|
||||
|
||||
var oauthInfo pg.UserOAuth
|
||||
copier.Copy(&oauthInfo, token)
|
||||
if err := copier.Copy(&oauthInfo, token); err != nil {
|
||||
return errors.Wrap(err, "failed to copy oauth info")
|
||||
}
|
||||
|
||||
user, err := f.userSvc.GetOrNew(c.Context(), tenantId, token.Openid, oauthInfo)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "failed to get user")
|
||||
}
|
||||
|
||||
claim := f.jwt.CreateClaims(jwt.BaseClaims{UID: uint64(user.ID)})
|
||||
claim := f.jwt.CreateClaims(jwt.BaseClaims{OpenID: user.OpenID})
|
||||
claim.ID = user.OpenID
|
||||
jwtToken, err := f.jwt.CreateToken(claim)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user