跳到主要内容

Sessions

Restore logged-in users, sign out, and manage access tokens.

备注

userInstanceESPRMUser from User sign in.

What This Module Does ?

Covers session lifecycle: read the current user, log out, clean up resources, and refresh or clear tokens stored by the SDK.

Use getLoggedInUser() on app start before showing the login screen. Use logout when the user signs out.

Expected outcome: Valid session restored automatically when tokens exist; clean teardown on logout.

Common Workflows

Get logged-in user

const authInstance = ESPRMBase.getAuthInstance();
const userInstance = await authInstance.getLoggedInUser();

if (userInstance) {
// Continue to home / devices
} else {
// Show login
}

Refreshes an expired access token when a refresh token is available.

Log out

await userInstance.logout(); // current session only
await userInstance.logout(true); // all sessions

Clean up resources

await userInstance.cleanUpResources();

Token helpers (static)

const accessToken = await ESPRMUser.getAccessToken();
const newAccessToken = await ESPRMUser.extendSession(refreshToken);
ESPRMUser.clearAllTokens();

Error Handling

try {
const user = await authInstance.getLoggedInUser();
} catch (error) {
console.error("Session restore failed:", error);
}

If restore fails, clear tokens and send the user to login.

Advanced Concepts

getLoggedInUser() returns null when no valid access or refresh token exists. getAccessToken() attempts refresh before failing—useful for custom HTTP clients that need a bearer token.

Best Practices

  1. Check session before routing on cold start
  2. Delete push endpoints on logout when you registered them at login
  3. Call cleanUpResources when tearing down user-scoped listeners or caches
  4. Avoid logging tokens in production builds

On this page