【发布时间】:2017-02-25 20:02:06
【问题描述】:
我在 nodejs 中有一些 API 和客户端的 React 应用程序。我尝试使用 jwt 令牌为我的 API/后台创建身份验证系统,我使用 jsonwebtoken 在服务器端创建和验证令牌,但我对客户端有一些疑问......现在登录时我将令牌保存在 localstorage ,然后使用 React-Router“onUpdate”检查本地存储是否有令牌,如果没有,我将重定向到登录,否则不附加任何内容,然后在我的应用程序上为每个 ajax 请求附加一个身份验证标头。
这是我的路由器
export const isLoggedIn = (nextState, replace) => {
console.log(localStorage.getItem('id_token'));
}
<Router history={browserHistory} onUpdate={isLoggedIn} >
<Route path="/" component={App}>
<IndexRoute component={Login.Login} />
<Route path="admin/" component={Dashboard} />
<Route path="admin/tutti" component={Users} />
</Route>
</Router>
我在这里登录
$.get('/login',credential, function (result) {
localStorage.setItem('id_token', result.token)
});
通用请求:
$.ajax({
url:"/api/users",
type:'GET',
contentType: "application/json",
success:function (result) {},
headers: {"x-access-token": localStorage.getItem('id_token')}
});
这是管理 React 身份验证流程的正确方法吗? 我的疑问是,在 isLoggedIn 上我需要以某种方式验证令牌吗?
非常感谢!
【问题讨论】:
标签: node.js authentication reactjs token jwt