UserSessionService
The UserSessionService provides an API for fetching and managing a user's login sessions across different devices and applications. This service is essential for building features that allow users to view their active login locations, see which devices have accessed their account, and manage those sessions.
For a practical guide on using this service, please refer to the Managing User Sessions guide.
Methods#
getMyLoginSessions()#
Retrieves a paginated list of the current user's own login sessions.
Parameters
Returns
Example
Fetching My Online Sessions
import { getBlockletSDK } from '@blocklet/js-sdk';
const sdk = getBlockletSDK();
async function fetchMySessions() {
try {
const sessionData = await sdk.userSession.getMyLoginSessions(
{},
{ page: 1, pageSize: 5, status: 'online' }
);
console.log('Online Sessions:', sessionData.list);
console.log('Total online sessions:', sessionData.paging.total);
} catch (error) {
console.error('Failed to fetch sessions:', error);
}
}
fetchMySessions();Example Response
{
"list": [
{
"id": "z8V...",
"appName": "My Blocklet",
"appPid": "my-blocklet-pid",
"lastLoginIp": "192.168.1.1",
"ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) ...",
"updatedAt": "2023-10-27T10:00:00.000Z",
"status": "online",
"userDid": "zNK..."
}
],
"paging": {
"page": 1,
"pageSize": 5,
"total": 1
}
}getUserSessions()#
Retrieves all login sessions for a specific user DID. This method is typically used in administrative contexts.
Parameters
Returns
Example
Fetching Sessions for a Specific User
import { getBlockletSDK } from '@blocklet/js-sdk';
const sdk = getBlockletSDK();
async function fetchUserSessions(userDid) {
try {
const sessions = await sdk.userSession.getUserSessions({ did: userDid });
console.log(`Sessions for user ${userDid}:`, sessions);
} catch (error) {
console.error('Failed to fetch user sessions:', error);
}
}
fetchUserSessions('zNK...userDid...'); // Replace with a valid user DIDExample Response
[
{
"id": "z8V...",
"appName": "My Blocklet",
"appPid": "my-blocklet-pid",
"lastLoginIp": "192.168.1.1",
"ua": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) ...",
"updatedAt": "2023-10-27T10:00:00.000Z",
"status": "online",
"userDid": "zNK..."
}
]loginByUserSession()#
Initiates a new login based on an existing user session ID. This can be used for features like seamless sign-in across related applications.
Parameters
Returns
Example
Logging In with an Existing Session
import { getBlockletSDK } from '@blocklet/js-sdk';
const sdk = getBlockletSDK();
async function loginWithSession(sessionDetails) {
try {
const newSessions = await sdk.userSession.loginByUserSession(sessionDetails);
console.log('Successfully logged in with new session:', newSessions[0]);
} catch (error) {
console.error('Login by session failed:', error);
}
}
const existingSession = {
id: 'session_id_to_use',
appPid: 'target_app_pid',
userDid: 'zNK...userDid...',
passportId: 'passport_id_string'
};
loginWithSession(existingSession);Data Structures#
The following are the primary data structures used by the UserSessionService.
UserSession#
Represents a single login session for a user in a specific application.
UserSessionUser#
Contains detailed information about the user associated with a session.
UserSessionList#
A paginated list of user sessions.
UserSessionQuery#
An object used to filter and paginate session queries.