This guide shows how to restore a MonoCloud session silently on app start and refresh the tokens stored on the session without an interactive sign-in.
signInSilent()refreshSession()refetchUserInfo()This guide assumes you've completed the JavaScript quickstart or the installation guide.
You should already have:
@monocloud/auth-web-js SDK installedMonoCloudWebJSClient initializedrefreshSession()requires a refresh token. Enable Refresh Tokens on the application to use theoffline_accessscope.
The SDK exposes two ways to keep a user signed in without an interactive sign-in:
| Method | What it does |
|---|---|
signInSilent() | Opens a hidden iframe with prompt=none to restore the session from the authorization server |
refreshSession() | Uses the stored refresh token to renew the tokens on the current session |
Call signInSilent() and catch MonoCloudOPError for the case where the user is not signed in at the authorization server.
import { MonoCloudOPError } from "@monocloud/auth-web-js";
try {
await client.signInSilent();
} catch (error) {
if (error instanceof MonoCloudOPError) {
// user is not signed in
} else {
throw error;
}
}
How it works:
signInSilent() opens a hidden iframe that hits the authorize endpoint with prompt=nonesignInSilent() throws MonoCloudOPErrorCall refreshSession() to run the refresh token grant and update the stored session with new tokens.
await client.refreshSession();
const session = await client.getSession();
How it works:
refreshSession() does not return tokens — read them from getSession() afterwards.
Pass refreshGrantOptions to scope the refresh to specific scopes or resources without changing the client-level configuration.
await client.refreshSession({
refreshGrantOptions: {
scopes: "openid profile email offline_access",
resource: "https://api.example.com",
},
});
The requested scopes and resources must already be allowed for the client. If they aren't, the authorization server rejects the request.
refreshSession() throws MonoCloudOPError when the refresh token has been revoked or has expired. Catch it and prompt the user to sign in again.
import { MonoCloudOPError } from "@monocloud/auth-web-js";
try {
await client.refreshSession();
} catch (error) {
if (error instanceof MonoCloudOPError) {
// refresh token is no longer valid
} else {
throw error;
}
}
Call refetchUserInfo() to fetch the updated profile from the UserInfo endpoint and update session.user in place.
await client.refetchUserInfo();
const session = await client.getSession();