Hanko Session Management Guide:About Hanko:Hanko is a modern open source authentication solution and the fastest way you integrate passkeys, 2FA, SSO, and more—with full control over your data. Move between self-hosted and Hanko Cloud anytime. No lock-in. Just Auth how it should be: secure, user friendly, and fully yours.What This Guide Covers: This guide demonstrates session management in applications using Hanko for authentication. You’ll learn to configure session lifetimes, implement session validation, handle multiple concurrent sessions, and customize session tokens with additional claims.Key Technologies:
- JSON Web Tokens (JWT)
- Session Cookies
- JWT Signature Validation
- Token Customization
- Hanko Cloud Console
- Active Hanko project configured
- Understanding of JWT concepts
- Basic knowledge of authentication flows
- Access to Hanko Cloud Console
- Frontend/backend development knowledge
- Configure session duration and limits
- Implement session validation logic
- Set up session termination methods
- Customize session tokens with claims
- Handle concurrent session management
- Configure cookie retention policies
- Monitor and revoke active sessions
Sessions and JSON Web Tokens
Hanko creates a session when users authenticate (registration or sign-in). Each session is represented by a JSON Web Token (JWT) that encodes user claims as JSON objects. All Hanko JWTs are signed using JSON Web Signature (JWS) standards for security. Authentication operations (login, registration) return session tokens in theX-Auth-Token
response header.
When using Hanko Elements or
the Hanko Frontend SDK, the session token is read from the
X-Auth-Token
header and used to set a cookie (name: hanko
) on the client-side. If you use neither Hanko
Elements nor the SDK, you have to take care of storing the session token yourself.Cookie
header or as a Bearer
token in the Authorization
header (Authorization: Bearer <JWT>
).
Upon session creation, a unique identifier is generated and stored in the database, linked to the corresponding user.
This identifier is also included in the session JWT under the session_id
claim. For subsequent requests, the API
validates the JWT signature and uses the session_id
to query the database for an active session match. This enables
remote session revocation. As a result, if a user is logged in on one device but has multiple active sessions across
other devices, the API’s Profile flow facilitates the listing and
explicit termination of sessions on those devices. This functionality is also accessible through the UI via the Hanko
Elements’ Profile Component.
Session validation
Validate sessions manually using thesessions/validate
endpoint. Send the session token in the request body for validation of expiry, signature, and session persistence.
The validation response includes session validity status, token expiry information, and associated user claims.
Alternatively, use the Hanko Frontend SDK for session validation. See Using the Frontend SDK for implementation details.
Session termination
Sessions can be terminated through various explicit and implicit methods, depending on your tenant configuration.Explicit user logout
Users can manually log out through the/logout
endpoint. Successful logout requests clear session cookies and terminate the associated session.
Limiting session duration
Sessions automatically expire based on configured duration settings. The session duration controls how long users remain authenticated after login. Hanko Cloud defaults to 12 hours, but you can configure anywhere from 1 minute to 1 month. Configure session duration:- Log in to Hanko Cloud and select your project
- Navigate to
Settings > Session
- Set your desired
Session duration
and clickSave
Limiting concurrent sessions
Sessions terminate automatically when exceeding the allowed concurrent session limit. Hanko defaults to 5 concurrent sessions - creating a new session invalidates the oldest existing session. Configure concurrent session limits:- Log in to Hanko Cloud and select your project
- Navigate to
Settings > Session
- Set your desired
Session limit
and clickSave
Limiting cookie retention period
You can configure the retention period of session cookies. Possible configurations are:- Persistent (default): This type sets the
Max-Age
attribute of the cookie to the specified session duration (in seconds). The cookie will be automatically deleted from the browser once the specified expiry time has been reached. - Session: This type sets the
Max-Age
attribute of the cookie to 0, resulting in the cookie being deleted from the browser when the browser tab or window is closed. - Prompt: If you use Hanko Elements (+1.3) then selecting this type results
in Hanko Elements displaying a
Stay signed in
checkbox on the login form which allows users to determine the cookie retention period: if the checkbox is checked, the cookie will be aPersistent
cookie, if it is unchecked the cookie will be aSession
cookie.
- Log in to Hanko Cloud and select your project.
- Navigate to
Settings > Session
. - Under
Stay signed in (cookie type)
, select the desired type and clickSave
.
Session self-service
There are a number of configurations options for your tenant that provide control over how users manage their session through the profile.Allowing session revocation
You can configure whether the Profile flow permits session revocation via the dedicatedsession_delete
action. If you are using
Hanko Elements version 1.3 or later, the Profile component provides interface
elements that allow users to revoke active sessions, excluding the session currently in use.
This session can be revoked manually through a manual logout.
To control self-service session revocation behaviour:
- Log in to Hanko Cloud and select your project.
- Navigate to
Settings > Session
. - Use the
Allow session revocation
toggle to allow or disallow end-user session revocation via profile.
Showing sessions on profile
You can control whether information about active sessions is returned in theProfile
flow.
If you use Hanko Elements (+1.3) then information about active sessions is
displayed in the Profile component.
To control whether information about active sessions is returned from the Profile
flow (and shown in Hanko Elements):
- Log in to Hanko Cloud and select your project.
- Navigate to
Settings > Session
. - Use the
Show sessions on profile
toggle to include information about active sessions in the profil API response (and the Hanko Elements profile component).
Session metadata retention
You can control whether active session metadata is persisted with a session and returned from session validation endpoints. This metadata currently includes the IP address and the user agent used to establish a session. To control the type of metadata retained with a session:- Log in to Hanko Cloud and select your project.
- Navigate to
Settings > Session
. - You can configure the following options:
- Use the
Acquire IP address
toggle to include information about the IP (IPv4/IPv6) address used to establish the session. - Use the
Acquire user agent
toggle to include information about the user agent used to establish the session.
- Use the
Session callbacks
You can hook into the session lifecycle by using the Hanko Frontend SDKs central client. See Using the Frontend SDK for details.Administrative session management
The Admin API allows administrators to create sessions, list active sessions and delete sessions for a specific user.Session token customization
Session tokens can be extended with custom claims by defining them using Go’s text/template syntax. These claims are evaluated during JWT generation and may include static values, dynamic expressions, or nested structures. To define custom claims:- Log in to Hanko Cloud and select your project.
- Navigate to
Settings > Session
. - Locate the
Customize session token
section. - Enter your custom claims in YAML format within the provided textarea.
- Click
Save
to apply your changes.
Accessing user data in templates
Each template has access to user data via the.User
field, which includes:
.User.UserID
: The user’s unique ID (string).User.Email
: Email details (optional).User.Email.Address
: The email address (string).User.Email.IsPrimary
: Indicates if the email is the primary address (boolean).User.Email.IsVerified
: Indicates if the email is verified (boolean)
.User.Username
: The user’s username (string, optional).User.Metadata
: The user’s public and unsafe metadata (optional).User.Metadata.Public
: The user’s public metadata (object).User.Metadata.Unsafe
: The user’s unsafe metadata (object)
Accessing user metadata
.User.Metadata.Public
and .User.Metadata.Unsafe
can be accessed and queried using
GJSON Path Syntax (try it out in the
playground).
Assume that a user’s public metadata consisted of the following data:
Ensure you use proper quoting when accessing metadata.
.User.Metadata.Public
and .User.Metadata.Unsafe
are function calls internally and and the given path argument must be a string, so it must be double quoted.
If you use use double quotes for your entire claim template then the path argument must be escaped, i.e.:"{{ .User.Metadata.Public \"display_name\" }}"
Example configuration
Important notes and limitations
- Custom claims are added at the top level of the session token payload.
- Claims with the following keys will be ignored because they are currently added to the JWT by default:
sub
iat
exp
aud
iss
email
username
session_id
- Templates must conform to valid Go text/template syntax. Invalid templates are logged and excluded from the generated token.
- Boolean strings (“true” or “false”) from templates are automatically converted to actual booleans.
Session JWT structure
The session JWT is a compact JWS. It is a string composed of three base64 URL safe encoded parts (header, payload and signature), separated by a dot (.
). The JWT payload contains the claims about a user.
JWT Header
The algorithm used for signing this token.
The key ID indicating which key was used to secure the token.
The media type of this token.
JWT Payload
The audience for which the JWT was created. It specifies the intended recipient or system that should accept this
JWT. When using Hanko Cloud, the
aud
will be your App URL
.An object containing information about the user’s email address.
The (UNIX) timestamp indicating when the JWT will expire.
The (UNIX) timestamp indicating when the JWT was created.
The ID of the session.
The ID of the user.
The username of the user (if set).
The payload further contains any custom claims that you have defined.