# Log out a user

## Log out a user

This page instructs you how to log users out of Passport and your application.

#### Pre-requisites[​](https://docs.x.immutable.com/docs/x/passport/log-out-user#pre-requisites) <a href="#pre-requisites" id="pre-requisites"></a>

* The user must be <mark style="color:blue;">logged into</mark> your application via Passport

### How to logout a user?[​](https://docs.x.immutable.com/docs/x/passport/log-out-user#how-to-logout-a-user) <a href="#how-to-logout-a-user" id="how-to-logout-a-user"></a>

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'`.

```
// 1. Initialise Passport and set the preferred logout mode
const passport = new Passport({
  logoutRedirectUri: 'http://localhost:3000',
  logoutMode: 'redirect', // defaults to 'redirect' if not set
  // ...
});

// 2. authenticate user
// ... refer to the "Enable user login" page for more information

// 3. Log the user out
await passport.logout();
```

#### Logout modes[​](https://docs.x.immutable.com/docs/x/passport/log-out-user#logout-modes) <a href="#logout-modes" id="logout-modes"></a>

**Redirect mode**[**​**](https://docs.x.immutable.com/docs/x/passport/log-out-user#redirect-mode)

The simplest approach is to use the `'redirect'` logout mode, which works as follows:

1. The application session is cleared by removing the JWT from the browser's local storage
2. The user is redirected to the Passport auth domain, where the Passport session is cleared
3. The user is redirected back to the specified `logoutRedirectUri`

**Silent mode**[**​**](https://docs.x.immutable.com/docs/x/passport/log-out-user#silent-mode)

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:

```
// Assume your application is hosted in http://localhost:3000
// 1. Initialise Passport and set the preferred logout mode
const pasport = new Passport({
  logoutRedirectUri: 'http://localhost:3000/silent-logout',
  logoutMode: 'silent',
  // ...
});

// 2. authenticate user
// ... refer to the "Enable user login" page for more information

// 3. Log the user out
await passport.logout();

// 4. Route handler for /silent-logout
await passport.logoutSilentCallback('http://localhost:3000');
```

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:

1. The application session is cleared by removing the JWT from the browser's local storage
2. A hidden iframe is created with the XpansionChain auth domain's logout URL where the Passport session is cleared
3. The iframe is redirected to the specified `logoutRedirectUri`
4. 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.
5. The hidden iframe is removed

### Next steps[​](https://docs.x.immutable.com/docs/x/passport/log-out-user#next-steps) <a href="#next-steps" id="next-steps"></a>

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 <mark style="color:blue;">Identity guide</mark>.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xpansionchain-1.gitbook.io/xpansionchain/products/passport/install-and-configure/log-out-a-user.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
