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.