The following code examples show you how to set up the user sign-out flow.
Implement token cleanup
The new SDK provides several methods to clean up tokens, depending on your use case:
Credential.revoke(): Revokes all available tokens from the authorization server. It works like the revoke(type:) method but with a default type of .all. As a result, it loops through all tokens calling revoke() on each of them in parallel.
- Note: The SDK keeps the token in storage so that you can refresh it to get a new access token. However, if the token is no longer usable, the SDK removes the token from storage. For example, if you revoke a refresh token and the associated access token is revoked.
Credential.revoke(type:): Revokes a specific token type (access token, refresh token, or device secret) from the authorization server. See Revoke tokens (opens new window).
- Notes:
- If you revoke an access token, the associated refresh token or device secret isn’t revoked.
- If you revoke a refresh token, the associated access token is revoked.
- Keeps the token in storage so that you can refresh it to get a new access token. However, if the token is no longer usable, the SDK removes the token from storage. For example, if you revoke a refresh token and the associated access token is revoked.
Credential.remove(): Clears the in-memory reference to the token and removes it from storage.
- Note: The SDK doesn’t revoke the token from the authorization server, so it can still be used.
When implementing your code, consider the following items:
- Always revoke all tokens: It’s always best to revoke all tokens. If the revoke fails, investigate the cause of the failure instead of removing the tokens from storage. For example, the failure could be due to a temporary network issue. In that case, it's better to try to revoke again to avoid a potential credential leak.
- Multiple accounts: If your app allows users to switch between multiple accounts or tenants, keep the following items in mind:
- Credential storage: The SDK can store multiple user credentials securely. If the credential that the
default property points to is removed, default is set to nil. As a result, assigning the default property, even to nil, doesn't remove a credential from storage. - Default credentials: The
Credential.default property can be used to determine which account is active. Switch the active user by assigning a different stored credential to the Credential.default property. - Sign-out scope: When a user signs out, you typically only want to sign out the active user. If you want to remove all stored sessions, you need to iterate over all stored credentials and revoke or remove each one.
Example token cleanup code
The following code example shows you how to implement local token clean-up as part of the user sign-out flow:
Example switch between user accounts code
The following code example shows you how to switch between user accounts:
End the Okta browser session (optional)
You don’t need to end the Okta browser session if either of the following are true:
- Your app uses DirectAuth (opens new window). That is, there isn’t a browser session.
- You signed in with
BrowserSignIn.shared?.ephemeralSession = true. That is, there are no persistent cookies.
Example end browser session code
See also
Validate SSO federation.