Skip to content

Session & Logout

Overview

The Session API (client.session) provides methods to check authentication state, and the Logout API handles both local and server-side logout scenarios.

Session Management

Checking Authentication State

const isAuthenticated = await client.session.isAuthenticated();
if (isAuthenticated) {
const user = await client.getUser();
console.log('Logged in as:', user.name);
} else {
console.log('Not authenticated');
}

Session Check

Perform a detailed session check that returns structured information:

const result = await client.session.check();
if (result.valid) {
console.log('Session is valid');
} else {
console.log('Session invalid:', result.error?.code);
}

SessionCheckResult

PropertyTypeDescription
validbooleanWhether the session is valid
errorAuthrimError | undefinedError details if the session is invalid

Getting User Information

Retrieve the authenticated user’s profile from the UserInfo endpoint:

const user = await client.getUser();
console.log(user.sub); // Subject identifier
console.log(user.name); // Full name
console.log(user.email); // Email address
console.log(user.email_verified); // Email verification status
console.log(user.picture); // Profile picture URL

UserInfo

PropertyTypeDescription
substringSubject identifier (unique user ID)
namestring | undefinedFull name
given_namestring | undefinedFirst name
family_namestring | undefinedLast name
nicknamestring | undefinedNickname
preferred_usernamestring | undefinedPreferred username
profilestring | undefinedProfile page URL
picturestring | undefinedProfile picture URL
emailstring | undefinedEmail address
email_verifiedboolean | undefinedWhether email is verified
phone_numberstring | undefinedPhone number
phone_number_verifiedboolean | undefinedWhether phone is verified
localestring | undefinedLocale
zoneinfostring | undefinedTime zone
updated_atnumber | undefinedLast update timestamp

Logout

Basic Logout

const result = await client.logout();
if (result.logoutUrl) {
// Redirect to the authorization server's logout endpoint
window.location.href = result.logoutUrl;
} else {
// Local-only logout completed
console.log('Logged out locally');
}

Logout Options

ParameterTypeDefaultDescription
postLogoutRedirectUristringURL to redirect after IdP logout
idTokenHintstringID token to identify the session
statestringState parameter for logout callback
revokeTokensbooleanfalseRevoke tokens before logout

Logout with Token Revocation

Revoke tokens at the authorization server before clearing the local session:

const result = await client.logout({
revokeTokens: true,
postLogoutRedirectUri: 'https://myapp.com/',
});
if (result.revocation) {
console.log('Revocation attempted:', result.revocation.attempted);
console.log('Access token revoked:', result.revocation.accessTokenRevoked);
console.log('Refresh token revoked:', result.revocation.refreshTokenRevoked);
}
if (result.logoutUrl) {
window.location.href = result.logoutUrl;
}

Logout with IdP Redirect (RP-Initiated Logout)

Redirect the user to the authorization server’s logout endpoint to end the server-side session:

const result = await client.logout({
postLogoutRedirectUri: 'https://myapp.com/',
idTokenHint: await client.token.getIdToken(),
});
if (result.logoutUrl) {
// Redirect to end the server-side session
window.location.href = result.logoutUrl;
}

Local-Only Logout

If you only want to clear the local session without contacting the authorization server:

// Set endpoints.endSession to null in config to disable server logout
const client = await createAuthrimClient({
// ...
endpoints: {
endSession: null,
},
});
// Or simply don't redirect to the logoutUrl
const result = await client.logout();
// result.localOnly === true

LogoutResult

PropertyTypeDescription
logoutUrlstring | undefinedURL to redirect for server-side logout
localOnlybooleanWhether only local cleanup was performed
revocationobject | undefinedToken revocation results
revocation.attemptedbooleanWhether revocation was attempted
revocation.accessTokenRevokedboolean | undefinedAccess token revocation result
revocation.refreshTokenRevokedboolean | undefinedRefresh token revocation result
revocation.errorError | undefinedRevocation error (non-fatal)

Front-Channel Logout

The SDK supports Front-Channel Logout, where the authorization server notifies all applications of a logout event via the browser. This is handled automatically when the session ends at the authorization server.

Session change events are emitted when logout is detected:

client.on('session:ended', (event) => {
console.log('Session ended');
// Clear UI state, redirect to login, etc.
});
client.on('session:logout_broadcast', (event) => {
console.log('Logout broadcast received');
// Another tab or application triggered logout
});

Complete Example

import { createAuthrimClient } from '@authrim/core';
const client = await createAuthrimClient({ /* ... */ });
// Check session on load
async function checkSession() {
const isAuthenticated = await client.session.isAuthenticated();
if (isAuthenticated) {
const user = await client.getUser();
showUserProfile(user);
} else {
showLoginButton();
}
}
// Logout handler
async function handleLogout() {
const result = await client.logout({
revokeTokens: true,
postLogoutRedirectUri: window.location.origin,
});
if (result.logoutUrl) {
window.location.href = result.logoutUrl;
} else {
showLoginButton();
}
}
// Listen for session changes
client.on('session:ended', () => {
showLoginButton();
});

Next Steps