Auth0 403禁止无效的授权代码



当我启动正在编写的应用程序时,它会从服务器加载适当的用户数据。当我刷新页面时,它不会加载我的表,并且它在服务器日志中声明授权代码无效,并抛出403错误。

if (authorisation_code && token_type !== null) {
// Post acces code and token 
function postAuthorisationCode() {
let data = {
authorisation_code: authorisation_code, 
token_type: token_type
};
let pac_url = "/lib/post-request-1.php"; 
fetch(uriRoot+pac_url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then((response) => {
// console.log(response);
response.text().then((rdata) => {
// console.log(rdata);
// Get length
let rdataLen = rdata.length; 
// Slice 200 status off by starting index 4.
let obj = rdata.slice(4, rdataLen);
// Parse string into an object. 
let returnObj = JSON.parse(obj);
// Set variables
let access_token = returnObj.access_token;

// Invoke postAccessToken function
postAccessToken(access_token);

})
})
}
// Function call
postAuthorisationCode();
function postAccessToken(access_token) {
let pat_url = "/lib/get-request-2.php";
// TODO: Eventually will include authorisation_code...
fetch(uriRoot+pat_url, { 
method: "POST",
headers: {
Accept: "application/json",
},
body: access_token
})
.then((response) => {
console.log(response);
response.text().then((rdata) => {
console.log(rdata);
})

userTable();


})
.catch((error) => {
console.log(error);
})
}

注意:userTable获取数据并将其放入一个表中!

有人能在我的代码中发现任何错误吗?为什么它在最初加载时可以工作,但在刷新后无法工作?

根据我对授权代码流的研究和理解:授权代码流似乎只在页面刷新后的第10阶段发生故障。服务器返回403无效的授权码,我相信这是因为授权码是一次性使用的。我需要实现一个刷新令牌,以便在不丢失数据的情况下刷新页面。我提出的解决方案如下:(这是因为我的业务要求刷新令牌尚未实现(

在调用刷新时,抛出一个警报,警告用户他们将丢失数据,并提示用户单击以取消操作或继续操作。如果用户决定继续操作,则用户将被重定向到登录页面,在那里他们可以登录,授权代码流中的操作顺序将按预期进行。

如果客户刷新,此代码将处理它:

<script type="text/javascript">
window.onbeforeunload = function() {
return "Data will be lost if you leave the page, are you sure?";
}
</script>
if (response.status !== 200) {
// console.log(response.status);

window.location.replace("...url here...");
}

第一段代码进入HTML文档,而第二段代码将进入JS。我的特别是在寻找任何不是200的状态代码。

最新更新