Integrating Session Management in your application
Session Management in OTPLESS allows developers to control user session durations, manage inactivity timeouts, validate session tokens, and revoke sessions in real-time for enhanced security and user experience.
Session Management
Session management is a critical aspect of authentication that ensures users remain securely logged in while interacting with an application. It involves maintaining a user’s authentication state between requests and managing session expiration or invalidation for security. OTPLESS offers two tailored solutions for session management, tailored to different use cases:
1. Server-Driven Management
In this workflow, the merchant’s backend server takes full control of managing user sessions. Here’s how it works:
- When a user logs in through OTPLESS, the merchant’s server communicates directly with the OTPLESS server to validate the authentication token.
- Once the user is authenticated, OTPLESS provides the necessary
sessionToken
andrefreshToken
needed for session management. - The
sessionToken
is meant for the frontend and should be securely stored in the application’s local storage, while therefreshToken
is intended for the backend and should be stored securely on the server. Further usage of both tokens is explained below. - OTPLESS provides APIs for all session related logic, such as validation, renewal, or revocation of a user’s session. All session-related logic is handled by the merchant’s backend by communicating with OTPLESS’ backend.
2. Frontend-Driven Management
In this workflow, OTPLESS Frontend SDKs manage user sessions directly within the client application (e.g., web or mobile apps). Here’s how it works:
- After the user logs in, the OTPLESS SDK authenticates the user and manages session details on the client side.
- The SDK handles token storage, session expiration, and renewal seamlessly, abstracting the complexities from the developer.
- This provides a lightweight and user-friendly solution that reduces dependency on server-side infrastructure and effort required by developers to implement a session management solution.
Choosing the Right Method
- Server-Driven Management offers flexibility and should be preferred by merchants requiring strict security controls and advanced flexibility in controlling their sessions.
- Frontend-Driven Management simplifies implementation, making it an excellent choice for teams focusing on rapid deployment and enhanced user experience.
In this workflow, the session management is handled by the merchant’s server, which communicates with the OTPLESS server to ensure that the user’s session is valid. This is the most secure and scalable method, as it centralizes session management at the server level.
Here’s a flowchart showcasing how session management and authentication with OTPLESS work together:
Summary of the flowchart:
-
User Authentication: When the user successfully authenticates via OTPLESS SDK on the merchant’s application, the merchant receives a
sessionToken
and arefreshToken
from OTPLESS. ThesessionToken
is meant for the frontend and should be securely stored in the application’s local storage, while therefreshToken
is intended for the backend and should be stored securely on the server. -
Action Trigger: When the user performs an action that requires session validation, the merchant’s frontend sends the
sessionToken
to merchant’s server, which then forwards thesessionToken
provided by OTPLESS during user authentication for validation to OTPLESS server. -
OTPLESS Server Validation: The OTPLESS server checks the validity of the
sessionToken
and responds to the merchant server with a validation result:- If valid: The action proceeds smoothly, indicating the user’s session is valid and the user can proceed with the action they wanted to perform.
- If invalid: The merchant server proceeds to the next step by sending the
refreshToken
to OTPLESS to validate therefreshToken
and create a newsessionToken
.
-
Refresh Token Validation: The OTPLESS server validates the
refreshToken
to determine if it is still valid:- If valid: OTPLESS generates a new
sessionToken
, which is returned to the merchant server. The merchant server updates the previously issuessessionToken
with the newly issuedsessionToken
. - If invalid: The user’s session is terminated, and they are required to authenticate again.
- If valid: OTPLESS generates a new
Key Terms
sessionToken
: A temporary JWT (JSON Web Token) key that allows the user to stay logged in without re-authenticating each time. It has a short expiry time.refreshToken
: A backup JWT (JSON Web Token) key used to request a newsessionToken
when the current one expires, keeping the user logged in. It has a long expiry time, typically the time for which you want the user to be kept logged in.
This method ensures that the session management process is both secure and resilient, leveraging centralized validation at the backend level.
Integration & Setup
Initial setup
- Configure OTPLESS dashboard: In Session Management section, enable Session Management and set the parameters as per your choice. Click the save button once session management is enabled and its parameters are set.
- Get OTPLESS credentials: Get the OTPLESS credentials from dashboard. Store them securely for using them in session management.
- Integrate OTPLESS on your application: Users must be authenticated via Otpless SDK to implement session management. To integrate Otpless SDK on your application, follow this guide.
Workflow
- Once the user successfully logs in via OTPLESS SDK on your application, securely store
sessionToken
andrefreshToken
provided by OTPLESS at your preferred location. - To perform any session related logic such as validation, renewal, or revocation of a user’s session, follow the below mentioned API endpoints.
API Endpoints
These are the API endpoints that OTPLESS provided to seamlessly integrate Session Management in your application.
-
Validate Session (
v1/sessions/validate-session
): Validates an existing user session usingsessionToken
issued by OTPLESS.- This API should be used whenever user performs an action on merchant app/website that requires their session to be valid.
- Example: A user is navigating the merchant’s app or website and tries to access a feature like viewing order history or account settings. Before allowing access, the app checks if their session is still valid by using this endpoint. If the session is invalid, the user may need to **create a new
sessionToken
.
-
Create New Session Token (
v1/sessions/get-new-session-token
): Creates a newsessionToken
using therefreshToken
if therefreshToken
is still valid.- This API should be used when the Validate Session API responds that the session is not valid. It returns a new
sessionToken
if therefreshToken
is valid. - Example: A user’s session expires while they are still actively using the app. Instead of forcing the user to log in again, the app uses this endpoint to generate a new
sessionToken
using the existingrefreshToken
, ensuring uninterrupted access.
- This API should be used when the Validate Session API responds that the session is not valid. It returns a new
-
Get Session Details (
v1/sessions/get-session-details
): Retrieves detailed information related to the user’s session, such as user phone number and network details.- This API should be used when the merchant wants to fetch the user’s details associated with the corresponding
sessionToken
. - Example: A merchant wants to personalize the user’s experience by displaying their name or phone number in the app. By calling this endpoint, the app can retrieve the session details associated with the
sessionToken
, such as the user’s phone number or email id, whichever the user has provided for authentication.
- This API should be used when the merchant wants to fetch the user’s details associated with the corresponding
-
Revoke Session (
v1/sessions/revoke-session
): Revokes a user’s session, typically called upon logout.- This API should be used when the merchant wants to terminate the user’s session. This API will revoke the corresponding
sessionToken
andrefreshToken
. - Example: A user logs out of the app, either manually or due to inactivity or there may have been suspicious activity on user’s device, due to which merchant may want to re-authenticate the user. This endpoint is used to securely terminate the user’s session, ensuring that their
sessionToken
can no longer be used for validation and a newsessionToken
cannot be created using therefreshToken
.
- This API should be used when the merchant wants to terminate the user’s session. This API will revoke the corresponding
Need help with your integration? Contact support for expert guidance and personalized assistance.
Was this page helpful?