Sessions
Session management in the @espressif/rainmaker-base-sdk handles user logout, resource cleanup, and token management operations.
The userInstance mentioned below refers to the ESPRMUser instance obtained during User Sign in.
Logged In User
Retrieve the currently logged-in user using the getLoggedInUser method. This method can be used to get the user instance of a logged-in user.
This method checks if the access token is available and valid. If the access token is expired, it attempts to refresh the session using the refresh token. If successful, it returns an instance of the ESPRMUser class containing the user's tokens. If no valid access token is found, it returns null.
try {
const authInstance = ESPRMBase.getAuthInstance();
const userInstance = await authInstance.getLoggedInUser();
if (userInstance) {
console.log("User is logged in:", userInstance);
// Use userInstance for further operations
} else {
console.log("No user is currently logged in");
}
} catch (error) {
console.error("Error getting logged in user:", error);
}
Returns: Promise<null | ESPRMUser> - A promise that resolves to an instance of ESPRMUser if the user is logged in, or null if not.
This method is useful for checking if a user session is still valid and for retrieving the user instance without requiring the user to log in again. If the access token is expired, the method will automatically attempt to refresh it using the refresh token.
Logout
Log out the current user, with an option to log out from all sessions using the logout method.
/*
- shouldLogoutFromAllSessions: Optional flag indicating whether to log out from all sessions. Defaults to false.
*/
try {
const response = await userInstance.logout(true);
console.log("User logged out successfully:", response);
} catch (error) {
console.error("Error logging out user:", error);
}
Clean Up Resources
Clean up resources associated with the user using the cleanUpResources method.
try {
await userInstance.cleanUpResources();
console.log("Resources cleaned up successfully");
} catch (error) {
console.error("Error cleaning up resources:", error);
}
Static Methods
Get Access Token
Retrieve the current access token. If the access token is expired, it attempts to extend the session using the refresh token using the getAccessToken static method.
try {
const accessToken = await ESPRMUser.getAccessToken();
console.log("Access token retrieved:", accessToken);
} catch (error) {
console.error("Error retrieving access token:", error);
}
Extend Session
Extend the session by exchanging the refresh token for a new access token and id token using the extendSession static method.
/*
- refreshToken: The refresh token used to extend the session.
*/
try {
const newAccessToken = await ESPRMUser.extendSession(refreshToken);
console.log("Session extended successfully. New access token:", newAccessToken);
} catch (error) {
console.error("Error extending session:", error);
}
Clear All Tokens
Clear all tokens from memory and local storage using the clearAllTokens static method.
try {
ESPRMUser.clearAllTokens();
console.log("All tokens cleared successfully");
} catch (error) {
console.error("Error clearing tokens:", error);
}