Skip to content

Commit

Permalink
refactor(client): extract storage key (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyijun authored Aug 30, 2022
1 parent d66ff91 commit b57b1c0
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
8 changes: 4 additions & 4 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ func (logtoClient *LogtoClient) IsAuthenticated() bool {
}

func (logtoClient *LogtoClient) GetRefreshToken() string {
return logtoClient.storage.GetItem("logto_refresh_token")
return logtoClient.storage.GetItem(StorageKeyRefreshToken)
}

func (logtoClient *LogtoClient) SetRefreshToken(refreshToken string) {
logtoClient.storage.SetItem("logto_refresh_token", refreshToken)
logtoClient.storage.SetItem(StorageKeyRefreshToken, refreshToken)
}

func (LogtoClient *LogtoClient) GetIdToken() string {
return LogtoClient.storage.GetItem("logto_id_token")
return LogtoClient.storage.GetItem(StorageKeyIdToken)
}

func (logtoClient *LogtoClient) SetIdToken(idToken string) {
logtoClient.storage.SetItem("logto_id_token", idToken)
logtoClient.storage.SetItem(StorageKeyIdToken, idToken)
}

func (logtoClient *LogtoClient) GetIdTokenClaims() (core.IdTokenClaims, error) {
Expand Down
8 changes: 8 additions & 0 deletions client/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package client

const (
StorageKeySignInContext = "logto_sign_in_context"
StorageKeyRefreshToken = "logto_refresh_token"
StorageKeyIdToken = "logto_id_token"
StorageKeyAccessTokenMap = "logto_access_token_map"
)
2 changes: 1 addition & 1 deletion client/handle_sign_in_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (logtoClient *LogtoClient) HandleSignInCallback(request *http.Request) erro
return fetchTokenErr
}

logtoClient.storage.SetItem("logto_sign_in_context", "")
logtoClient.storage.SetItem(StorageKeySignInContext, "")

accessToken := AccessToken{
Token: codeTokenResponse.AccessToken,
Expand Down
2 changes: 1 addition & 1 deletion client/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (logtoClient *LogtoClient) persistAccessTokenMap() {
if err != nil {
return
}
logtoClient.storage.SetItem("logto_access_token_map", string(accessTokenMapJsonString))
logtoClient.storage.SetItem(StorageKeyAccessTokenMap, string(accessTokenMapJsonString))
}

func (logtoClient *LogtoClient) createRemoteJwks(jwksUri string) (*jose.JSONWebKeySet, error) {
Expand Down
2 changes: 1 addition & 1 deletion client/sign_in.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (logtoClient *LogtoClient) SignIn(redirectUri string) (string, error) {
return "", marshalErr
}

logtoClient.storage.SetItem("logto_sign_in_context", string(signInContextJsonValue))
logtoClient.storage.SetItem(StorageKeySignInContext, string(signInContextJsonValue))

return signInUri, nil
}
6 changes: 3 additions & 3 deletions client/sign_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ func (logtoClient *LogtoClient) SignOut(postLogoutRedirectUri string) (string, e
refreshToken := logtoClient.GetRefreshToken()

logtoClient.accessTokenMap = make(map[string]AccessToken)
logtoClient.storage.SetItem("logto_access_token_map", "")
logtoClient.storage.SetItem("logto_refresh_token", "")
logtoClient.storage.SetItem("logto_id_token", "")
logtoClient.storage.SetItem(StorageKeyAccessTokenMap, "")
logtoClient.storage.SetItem(StorageKeyRefreshToken, "")
logtoClient.storage.SetItem(StorageKeyIdToken, "")

oidcConfig, fetchOidcConfigErr := logtoClient.fetchOidcConfig()

Expand Down

0 comments on commit b57b1c0

Please sign in to comment.