首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Auth0 isAuthenticated()始终为false

Auth0 isAuthenticated()始终为false
EN

Stack Overflow用户
提问于 2022-06-13 08:09:03
回答 1查看 634关注 0票数 2

我使用Extjs,我使用本教程来设置应用程序和auth0。以下是登录的代码:

代码语言:javascript
运行
复制
userLogin: function() {     
    //Create auth0 client
    createAuth0Client({
      domain: ".....auth0.com",
      client_id: ".....",
      useRefreshTokens: true
    }).then(function(auth0) {
        try {
            //Check if the user is authenticated, if not authenticate him, if yes insert his token in every ajax request
            auth0.isAuthenticated().then(function(authenticated) {
                if(!authenticated)
                {                      
                    auth0.loginWithRedirect({ redirect_uri: window.location.origin }).then();
                }
                else{
                    auth0.getTokenSilently().then(function(token) {
                        Ext.Ajax.setDefaultHeaders({ 'Authorization' : 'Bearer ' + token });
                    });
                }
            })
        } catch (err) {
            console.log("Log in failed", err);
        }
    });
}

在第一次尝试中,isAuthenticated是false (正常行为),因此用户被重定向到auth0登录提示,用户输入他的凭证并登录,auth0重定向到应用程序,现在isAuthenticated仍然是假的,用户被重定向到auth0但没有登录提示,因为他已经登录,重定向回应用程序,现在无限循环开始.

auth0中的应用程序设置为SPA (单页应用程序)。试图更改缓存位置,但没有更改任何内容。

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-30 09:53:57

我想通了。您需要使用auth0.handleRedirectCallback()函数:

代码语言:javascript
运行
复制
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);
}

或者,您可以使用loginWithPopup()

代码语言:javascript
运行
复制
if(!authenticated) {                      
    auth0.loginWithPopup().then(token => {
        auth0.getUser().then(user => {
            console.log(user);
        });
    })
 }
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72599560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档