Login with a redirect
The login function sends the user to the Secrecy authentication page. This page covers the redirect flow and the options that control what comes back.
Log in with a redirect
With redirect: true, the browser navigates to the Secrecy authentication page. The promise never resolves with a client on that first call, because the page is left before it can. Use path and backPath so the user is sent back where they were.
auth.ts
const loginWithRedirect = async (): Promise<SecrecyClient | null> => {
try {
// The page navigates away, this resolves null
return await login({
appId: '<YOUR_APP_ID>',
redirect: true,
// Where the user is sent back after the login
path: window.location.pathname,
backPath: document.referrer,
});
} catch (error) {
console.error(error);
return null;
}
};Request the user email
The scopes option declares what your application asks for. Set { email: true } to get the user email along with the session.
Force a login for a given user
The context option tells Secrecy which user or organization is expected. If a client is already stored for a different user, it is discarded and a new login starts. Use forceLogin to always start a new login, whatever is stored.
auth.ts
const loginAs = async (userId: string): Promise<SecrecyClient | null> => {
try {
return await login({
appId: '<YOUR_APP_ID>',
redirect: true,
// A mismatch with the stored client forces a new login
context: { userId },
// Always start a new login, even if a client is stored
forceLogin: false,
});
} catch (error) {
console.error(error);
return null;
}
};