Log out a user
Last updated
Last updated
This page instructs you how to log users out of Passport and your application.
The user must be logged into your application via Passport
When a user authenticates through Passport, there are two "sessions" you need to account for. The Passport session layer maintains a session between the user and Passport in auth.
XpansionChain.com
, and the application session layer maintains a session between the user and your application through JWTs.
In order to log out a user from your application and Passport, you can use the logout
function on the Passport instance.
Depending on the logout mode you specify, the user will be redirected to the Passport auth domain or silently logged out within your application. You can specify the logout mode when you initialize the Passport instance by setting the logoutMode
to either 'redirect'
or 'silent'
.
The simplest approach is to use the 'redirect'
logout mode, which works as follows:
The application session is cleared by removing the JWT from the browser's local storage
The user is redirected to the Passport auth domain, where the Passport session is cleared
The user is redirected back to the specified logoutRedirectUri
Alternatively, you can use the 'silent'
logout mode which is less intrusive and does not require a top-level redirect. However, in order to use this mode, you must set up a callback URL that invokes the logoutSilentCallback
function:
The logoutSilentCallback
function accepts a single parameter, which is the URL of the page that initiated the logout. In other words, if you initiate the logout from http://localhost:3000
, then you should pass http://localhost:3000
.
The 'silent'
logout mode works as follows:
The application session is cleared by removing the JWT from the browser's local storage
A hidden iframe is created with the XpansionChain auth domain's logout URL where the Passport session is cleared
The iframe is redirected to the specified logoutRedirectUri
Your application handles the logoutRedirectUri
request and invokes the logoutSilentCallback
function. This causes the iframe to emit an event to the parent window which completes the logout flow.
The hidden iframe is removed
Now that you've integrated Passport into your application & set up the login flow, you are ready to integrate more of the Passport functionality. Learn more about how authentication works in Passport in the Identity guide.
Redirect mode
Silent mode