[英]React doesn't refresh state in LocalStorage
你好吗?
我正在使用 React 和 Cognito 托管 UI 进行概念验证。 我在这里使用反应路由器 dom 有此代码:
return (
<div>
<main>
<Switch>
< Route path='/' exact>
{
(localStorage.getItem('access_token') && localStorage.getItem('access_token')!=='undefined') ? history.replace('/home'): (
<div>
<b>You are not logged in Igor, please log in using Google or User and Password. </b>
<b></b>
<a href="https://<MY_DOMAIN>.auth.us-east-1.amazoncognito.com/login?client_id=<MY_CLIENT_ID>&response_type=code&scope=aws.cognito.signin.user.admin+email+openid+phone+profile&redirect_uri=<MY_CLOUDFRONT_DISTRIBUTION_APEX>">Sign in</a>
</div>
)
}
</Route>
<Route path='/calculator'>
<Calculator/>
</Route>
<Route path='/home/'>
<Welcome/>
</Route>
<Route path='/welcome/'>
<RedirectPage/>
</Route>
</Switch>
</main>
</div>
);
我使用 S3 作为 static 网站托管,这就是为什么我指向 S3 云端分发顶点。 然后,在我的应用程序 function(App.js 文件中的 residex)中,我每次用户访问时都进行此验证,以验证这是否来自重定向,如果出现,我将 auth_code 交换为 JWT 令牌,这将是与 API 网关一起使用:
const location = useLocation();
const history = useHistory();
let authCode = new URLSearchParams(location.search).get('code');
console.log(authCode)
if(authCode){
fetch('https://<GATEWAY_ID>.execute-api.us-east-1.amazonaws.com/dev/auth',{
method: 'POST',
body: JSON.stringify(authCode),
headers: {
'Content-Type': 'application/json'
}
}).then((res)=>{return res.json()}).then(data=>{
console.log('access token is:');
console.log(data.result['access_token']);
console.log('id token is:');
console.log(data.result['id_token']);
localStorage.setItem('access_token',data.result['access_token']);
history.replace('/home')
});
}
问题是,在我使用谷歌或登录名和密码进行身份验证后,我收到代码,我在后端使用 lambda(在 /dev/auth 路由上,网关后面)在 cognito 上进行身份验证,lambda 执行身份验证好吧,但是我的前端页面进入了一个循环,并且在打印 null 很多次之后(从 console.log(authCode) 行,我收到一个错误,告诉我在一个循环中)。 我是反应的新手,我做这个 POC 是为了理解这个概念并向客户展示这个过程。
提前致谢。 我无法完整共享代码,因为它们存储在私人公司存储库中。
我的完整代码:
import React from 'react';
import { Route, Switch } from 'react-router-dom';
import Calculator from './pages/Calculator';
import Welcome from './pages/Welcome';
import {useHistory, useLocation} from 'react-router-dom';
import RedirectPage from './pages/Loading';
function App() {
const location = useLocation();
const history = useHistory();
let authCode = new URLSearchParams(location.search).get('code');
console.log(authCode)
if(authCode){
fetch('https://<GATEWAY_ID>.execute-api.us-east-1.amazonaws.com/dev/auth',{
method: 'POST',
body: JSON.stringify(authCode),
headers: {
'Content-Type': 'application/json'
}
}).then((res)=>{return res.json()}).then(data=>{
console.log('access token is:');
console.log(data.result['access_token']);
console.log('id token is:');
console.log(data.result['id_token']);
localStorage.setItem('access_token',data.result['access_token']);
history.replace('/home')
});
}
return (
<div>
<main>
<Switch>
< Route path='/' exact>
{
(localStorage.getItem('access_token') && localStorage.getItem('access_token')!=='undefined') ? history.replace('/home'): (
<div>
<b>You are not logged in Igor, please log in using Google or User and Password. </b>
<b></b>
<a href="<HOSTED_UI_COGNITO>">Sign in</a>
</div>
)
}
</Route>
<Route path='/calculator'>
<Calculator/>
</Route>
<Route path='/home/'>
<Welcome/>
</Route>
<Route path='/welcome/'>
<RedirectPage/>
</Route>
</Switch>
</main>
</div>
);
}
export default App;
我可以看到您已经在功能组件中编写了 api 调用代码,我假设您只想点击 api 一次以实现您可以在 useEffect 中编写您的代码
useEffect(()=>{
if(authCode){
fetch('https://<GATEWAY_ID>.execute-api.us-east-1.amazonaws.com/dev/auth',{
method: 'POST',
body: JSON.stringify(authCode),
headers: {
'Content-Type': 'application/json'
}
}).then((res)=>{return res.json()}).then(data=>{
console.log('access token is:');
console.log(data.result['access_token']);
console.log('id token is:');
console.log(data.result['id_token']);
localStorage.setItem('access_token',data.result['access_token']);
history.replace('/home')
});
}
},[])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.