我使用Extjs,我使用本教程来设置应用程序和auth0。以下是登录的代码:
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 (单页应用程序)。试图更改缓存位置,但没有更改任何内容。
有什么想法吗?
发布于 2022-06-30 09:53:57
我想通了。您需要使用auth0.handleRedirectCallback()
函数:
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()
if(!authenticated) {
auth0.loginWithPopup().then(token => {
auth0.getUser().then(user => {
console.log(user);
});
})
}
https://stackoverflow.com/questions/72599560
复制相似问题