Implementing a Logout
The AuthConnect.logout()
method requires an AuthResult
in order to properly perform a logout operation. Thus, a common logout implementation will:
- Get the current
AuthResult
from session storage (refreshing if necessary). - Perform the logout.
- Clear the session storage.
The code may look something like the following:
_10 async logout(): Promise<void> {_10 const authResult = await this.getAuthResult();_10 await AuthConnect.logout(this.provider, authResult);_10 await this.saveAuthResult(null);_10 }
In many cases the code may not be able to obtain an AuthResult
. For example, if the AuthResult
is stored in a locked vault that the current user cannot unlock, the application may give the user the option to log in again. In that case, the application will:
- Clear the current session.
- Logout the current user.
- Show a login page.
For example:
_10 async redoClicked() {_10 await this.sessionVault.clear();_10 await this.authService.logout();_10 this.navController.navigateRoot('/login');_10 }
In this case, the logout()
implementation that we defined will not be able to find an AuthResult
and will thus fail to operate properly.
In such cases, we can manufacture an AuthResult
that will contain enough information to perform a logout. This is possible because the logout does not rely on any of the tokens that are stored in the AuthResult
. It only relies on some of the other data that is cached there.
In such cases, use AuthConnect.buildAuthResult()
to generate a AuthResult
that contains the required connection options but does not contain any tokens. Use the same AuthOptions
that are used when calling AuthConnect.login()
.
_10 async logout(): Promise<void> {_10 let authResult = await this.getAuthResult();_10 if (!authResult) {_10 authResult = await AuthConnect.buildAuthResult(this.provider, this.authOptions, {});_10 }_10 await AuthConnect.logout(this.provider, authResult);_10 await this.saveAuthResult(null);_10 }
If you would like to see how this code in the context of an application, please have a look at the Identity Vault and Auth Connect demos: