We are using Auth0 account with tenant ID, having application of type ‘Single Page Application’.
Web application details where we have configured login of auth0,
“vue”: “^3.2.22”,
“typescript”: “^4.5.2”,
auth0-spa-js: “^1.19.2”
Also using new “Organization” feature of auth0
Error is:
Getting an error Invalid state at client.handleRedirectCallback(), but not always.
Code snippet:
type AuthenticationState = {
initializing: boolean;
loginActive: boolean;
isAuthenticated: boolean;
user: User | undefined;
accessToken: string | undefined;
error: Error | undefined;
};
const state = reactive<AuthenticationState>({
initializing: false,
loginActive: false,
isAuthenticated: false,
user: undefined,
accessToken: undefined,
error: undefined,
});
const client = new Auth0Client({
domain: "",
client_id: "",
display: "page",
audience: "",
scope: "openid profile email",
redirect_uri: window.location.origin
});
async function setupAuth(cb: (appState: AppState) => void) {
try {
state.loginActive = true;
console.info("Checking session...");
await client.checkSession({ scope: "openid profile email" });
if (
window.location.search.includes("code=") &&
window.location.search.includes("state=")
) {
const { appState } = await client.handleRedirectCallback();
cb(appState);
}
} catch (e: any) {
console.error(`Error setting up auth - ${e}`);
if (e instanceof Error) {
state.error = e;
} else {
state.error = new Error(`Failed to setup auth - ${e}`);
}
throw state.error;
} finally {
state.loginActive = false;
state.isAuthenticated = await client.isAuthenticated();
state.user = await client.getUser();
if (state.isAuthenticated) {
state.accessToken = await client.getTokenSilently();
}
}
}