refactor: add LogoutByToken() and SetJwtSecretKey()

This commit is contained in:
weloe
2023-05-19 22:40:22 +08:00
parent c50b10f5f6
commit dc9f137435
4 changed files with 12 additions and 3 deletions

View File

@@ -49,6 +49,10 @@ type TokenConfig struct {
CookieConfig *CookieConfig CookieConfig *CookieConfig
} }
func (t *TokenConfig) SetJwtSecretKey(secretKey string) {
t.JwtSecretKey = secretKey
}
func DefaultTokenConfig() *TokenConfig { func DefaultTokenConfig() *TokenConfig {
return &TokenConfig{ return &TokenConfig{
TokenStyle: "uuid", TokenStyle: "uuid",

View File

@@ -301,7 +301,7 @@ func (e *Enforcer) Logout(ctx ctx.Context) error {
tokenConfig.CookieConfig.Domain) tokenConfig.CookieConfig.Domain)
} }
err := e.logoutByToken(token) err := e.LogoutByToken(token)
if err != nil { if err != nil {
return err return err

View File

@@ -14,6 +14,7 @@ type IEnforcer interface {
Login(id string, ctx ctx.Context) (string, error) Login(id string, ctx ctx.Context) (string, error)
LoginByModel(id string, loginModel *model.Login, ctx ctx.Context) (string, error) LoginByModel(id string, loginModel *model.Login, ctx ctx.Context) (string, error)
Logout(ctx ctx.Context) error Logout(ctx ctx.Context) error
LogoutByToken(token string) error
IsLogin(ctx ctx.Context) (bool, error) IsLogin(ctx ctx.Context) (bool, error)
IsLoginById(id string) (bool, error) IsLoginById(id string) (bool, error)
GetLoginId(ctx ctx.Context) (string, error) GetLoginId(ctx ctx.Context) (string, error)

View File

@@ -76,8 +76,8 @@ func (e *Enforcer) ResponseToken(tokenValue string, loginModel *model.Login, ctx
return nil return nil
} }
// logoutByToken clear token info // LogoutByToken clear token info
func (e *Enforcer) logoutByToken(token string) error { func (e *Enforcer) LogoutByToken(token string) error {
var err error var err error
// delete token-id // delete token-id
id := e.GetIdByToken(token) id := e.GetIdByToken(token)
@@ -143,3 +143,7 @@ func (e *Enforcer) spliceSessionKey(id string) string {
func (e *Enforcer) spliceTokenKey(id string) string { func (e *Enforcer) spliceTokenKey(id string) string {
return e.config.TokenName + ":" + e.loginType + ":token:" + id return e.config.TokenName + ":" + e.loginType + ":token:" + id
} }
func (e *Enforcer) SetJwtSecretKey(key string) {
e.config.JwtSecretKey = key
}