isAuthenticated() is always false

You will need to use the auth0.handleRedirectCallback() function to handle success and error responses from Auth0.

try {
    auth0.isAuthenticated().then(async function (authenticated) {
        if (!authenticated) {
            const query = window.location.search;
            const shouldParseResult = query.includes("code=") && query.includes("state=");
            if (shouldParseResult) {
                console.log("> Parsing redirect");
                try {
                    const result = await auth0.handleRedirectCallback();
                    console.log("Logged in!");
                } catch (err) {
                    console.log("Error parsing redirect:", err);
                }
                window.history.replaceState({}, document.title, "/");
            } else {
                auth0.loginWithRedirect({ redirect_uri: window.location.origin });
            }
        } else {
            auth0.getTokenSilently().then(function (token) {
                Ext.Ajax.setDefaultHeaders({ 'Authorization': 'Bearer ' + token });
            });
        }
    })
} catch (err) {
    console.log("Log in failed", err);
}

Alternatively, you can the useloginWithPopup() function:

if(!authenticated) {                      
    auth0.loginWithPopup().then(token => {
        auth0.getUser().then(user => {
            console.log(user);
        });
    })
 }
1 Like