Skip to content

Commit

Permalink
feat: fix user cannot logout issue about bug in GetSessionToken()
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Mar 17, 2024
1 parent bdf9864 commit ae1634a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 25 deletions.
10 changes: 5 additions & 5 deletions controllers/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -435,16 +435,16 @@ func (c *ApiController) GetAccount() {
return
}

token := c.GetSessionToken()
if token == nil {
token, err = object.GetTokenForExtension(user, c.Ctx.Request.Host)
accessToken := c.GetSessionToken()
if accessToken == "" {
accessToken, err = object.GetAccessTokenByUser(user, c.Ctx.Request.Host)
if err != nil {
c.ResponseError(err.Error())
return
}
c.SetSessionToken(token)
c.SetSessionToken(accessToken)
}
u.AccessToken = token.AccessToken
u.AccessToken = accessToken

resp := Response{
Status: "ok",
Expand Down
18 changes: 8 additions & 10 deletions controllers/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,13 @@ func (c *ApiController) GetSessionUsername() string {
return user.(string)
}

func (c *ApiController) GetSessionToken() *object.Token {
tokenValue := c.GetSession("token")
var token *object.Token
var ok bool
if token, ok = tokenValue.(*object.Token); !ok {
token = nil
func (c *ApiController) GetSessionToken() string {
accessToken := c.GetSession("accessToken")
if accessToken == nil {
return ""
}

return token
return accessToken.(string)
}

func (c *ApiController) GetSessionApplication() *object.Application {
Expand All @@ -153,7 +151,7 @@ func (c *ApiController) ClearUserSession() {
}

func (c *ApiController) ClearTokenSession() {
c.SetSessionToken(nil)
c.SetSessionToken("")
}

func (c *ApiController) GetSessionOidc() (string, string) {
Expand Down Expand Up @@ -182,8 +180,8 @@ func (c *ApiController) SetSessionUsername(user string) {
c.SetSession("username", user)
}

func (c *ApiController) SetSessionToken(token *object.Token) {
c.SetSession("token", token)
func (c *ApiController) SetSessionToken(accessToken string) {
c.SetSession("accessToken", accessToken)
}

// GetSessionData ...
Expand Down
5 changes: 0 additions & 5 deletions object/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ func InitDb() {
}

initWebAuthn()
initToken()
}

func getBuiltInAccountItems() []*AccountItem {
Expand Down Expand Up @@ -310,10 +309,6 @@ func initWebAuthn() {
gob.Register(webauthn.SessionData{})
}

func initToken() {
gob.Register(&Token{})
}

func initBuiltInUserModel() {
model, err := GetModel("built-in/user-model-built-in")
if err != nil {
Expand Down
11 changes: 6 additions & 5 deletions object/token_oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -727,18 +727,19 @@ func GetWechatMiniProgramToken(application *Application, code string, host strin
return token, nil, nil
}

func GetTokenForExtension(user *User, host string) (*Token, error) {
func GetAccessTokenByUser(user *User, host string) (string, error) {
application, err := GetApplicationByUser(user)
if err != nil {
return nil, err
return "", err
}
if application == nil {
return nil, fmt.Errorf("the application for user %s is not found", user.Id)
return "", fmt.Errorf("the application for user %s is not found", user.Id)
}

token, err := GetTokenByUser(application, user, "profile", "", host)
if err != nil {
return nil, err
return "", err
}
return token, nil

return token.AccessToken, nil
}

0 comments on commit ae1634a

Please sign in to comment.