【发布时间】:2020-01-18 23:36:10
【问题描述】:
我有一个带有 reactjs 的应用程序,它有一个登录页面,在用户成功登录后,我将令牌保存在 localstorge 中并在我的浏览器中保存一个 cookie,它在除 IE (11) 之外的所有浏览器中都能正常工作,我在之后跟踪再次登录IE清除localstorage,然后用户返回登录页面;我真的不知道该怎么办:(
这是我的登录组件:
export default function Login(props){
const [values, setValues] = useState({
Username : '' ,
Password : '' ,
Remember : false ,
error : false
});
const loginBtn = React.createRef();
const [token , setToken] = useState(null);
const [output , setOutput] = useState(null);
const handleChange = name => event => {
// console.log(name);
setValues({
...values ,
[name] : event.target.value
});
};
console.log('cookie (out): ' , document.cookie);
console.log('localstorage (out): ' , localStorage.getItem('access_Token'));
const loginClick = () => {
axios.post('/dashboard/auth' , {"username" : values.Username , "password" : values.Password})
.then(response => {
localStorage.setItem('access_Token' , response.data.access_Token);
document.cookie = 'electiondashboard=1';
setOutput(<Redirect to='/dashboard' />);
console.log('cookie : ' , document.cookie);
console.log('localstorage : ' , localStorage.getItem('access_Token'));
})
.catch(error => {
console.log('in login : ' , error);
setValues({
...values ,
error : true
});
});
};
const onKeyDownhandler = (e) => {
if(e.keyCode === 13){
e.preventDefault();
loginBtn.current.click();
}
};
return (
<Fragment>
<div className={myClasses.DRT_Login}>
<div className={myClasses.DRT_Box}>
<h4 className={myClasses.DRT_Title}>
به سامانه انتخاباتی دورتال
<br/>
خوش آمدید
</h4>
<form>
<div>
<label>
ایمیل یا نام کاربری خود را وارد کنید
</label>
<input type='text' name='Username'
placeholder='Enter Username Or Email Address'
onChange={handleChange('Username')}
onKeyDown={e => onKeyDownhandler(e)}/>
</div>
<div>
<label>رمز ورود را وارد کنید</label>
<input type='password' name='Password'
placeholder='Enter Password'
onChange={handleChange('Password')}
onKeyDown={e => onKeyDownhandler(e)}/>
</div>
<Grid container>
<Grid item xs={12} sm={6}>
<a href="http://google.com" className={myClasses.DRT_ForgotPass}>
رمز عبور خود را فراموش کرده اید ؟
</a>
</Grid>
</Grid>
<div>
<ButtonTemp
type='link'
mode='primary'
styles={{width : '100%' , fontWeight : 'normal' , marginTop : '20px'}}
onclick={loginClick}
reactRef={loginBtn}>
ورود
</ButtonTemp>
</div>
{
values.error ?
(<AlertTemp type='danger' styles={{fontSize : '13px'}}>
نام کاربری یا رمز عبور اشتباه است
</AlertTemp>) : ''
}
</form>
</div>
</div>
{output}
</Fragment>
);
};
这是我的索引组件检查令牌然后指导用户何时可以去:
export default function Index(props){
const [token , setToken] = useState(localStorage.getItem('access_Token'));
console.log('in index localstorage : ' , localStorage.getItem('access_Token'));
console.log('in index localstorage eeeee : ' , (localStorage.getItem('access_Token') === null) );
useEffect(() => {
if(localStorage.getItem('access_Token'))
setToken(localStorage.getItem('access_Token'));
else
setToken(null);
} , [token]);
return (
<Fragment>
<Route exact path='/dashboard' component={Layout} />
<Route exact path='/dashboard/cartable' component={Layout} />
<Route path='/dashboard/cartable/requests' component={Layout} />
<Route path='/dashboard/cartable/approved' component={Layout} />
<Route path='/dashboard/cartable/rejected' component={Layout} />
<Route path='/dashboard/elections-management' component={Layout} />
<Route path='/dashboard/statistics/assignee' component={Layout} />
<Route path='/dashboard/statistics/participation' component={Layout} />
<Route path='/dashboard/errors' component={Layout} />
{
token === null ?
<Route path='/dashboard/login' component={Login} />
: <Redirect to='/dashboard' />
}
</Fragment>
);
};
【问题讨论】:
-
尝试在IE浏览器上测试this sample,如果本地存储仍然不能正常工作,则说明问题与IE浏览器有关,尝试重置IE浏览器设置或重新安装IE浏览器。如果本地存储运行良好,可能问题与您的代码有关,请尝试在您的代码中添加一些调试器或使用 F12 开发人员工具调试您的代码并检查本地存储(您是否使用 clear 方法清除本地存储)。
-
感谢您的帮助,我会试试的
标签: reactjs internet-explorer cookies local-storage