Session Management
How to use OTPless Session Management
Learn how to manage OTPless user sessions effectively using session tokens for secure and seamless authentication.
Starting a Session
To begin a user session, use the OTPless Auth SDK to authenticate the user. After successful authentication, the SDK response includes a sessioninfo
or you can call OTPlessVerifySession function to retrive the sessioninfo if it exists object containing the following:
sessionId
: The primary session identifier.sessionToken
: A JSON Web Token for secure session validation.refreshToken
: Used to refresh expired sessions.
In our case sessionId
is basically session token
and sessionToken
is session jwt token
.
Steps:
- Enable Session Management:
- Go to the OTPless dashboard.
- Set the following configurations:
- Session Duration: The total duration a session remains active.
- Maximum Lifetime: The maximum time a session can exist.
- Inactivity Timeout: The time after which an inactive session is terminated.
- Store Session Information:
- Save the
sessionId
andsessionToken
client-side (e.g., in cookies or local storage) securely.
- Save the
Authenticating a Session
Before performing any action that requires authorization, verify the session validity.
Steps:
- Call the authenticate session API with the
sessionId
or UsesessionToken
JWT to verify it locally. - Validate the response:
- If the session is valid, use the
user_id
from the response to identify the user. - Send the
sessionId
orsessionToken
in a session cookie for subsequent requests.
- If the session is valid, use the
- If the session is invalid:
- Clear the session cookie to log the user out.
- Do not process the unauthorized request.
Recommendation: Follow OWASP’s guidelines on secure cookie storage to ensure sensitive information is protected.
Revoking a Session
To terminate a user session:
- Call the revoke session API or Use SDK Logout Function with the
session_token
. - Ensure that the session cookie is cleared client-side to prevent further use.