如何在用户的信息存储记录?用户、如何在、信息

2023-09-13 02:50:46 作者:款少爷

我有角的应用程序,用户可以登录使用电子邮件/密码,服务器将返回令牌。

I have angular app, user can login using email/password, and server will return token.

现在我有角的服务让用户的数据,但如果重新加载页面,用户登录了。

Now I have angular service to keep user's data, but if page reload, user logged out.

我应该存储的道理,为了让更多的API调用?

Should I store that token, in order to make further API calls?

如何以及我应该在哪里存储令牌?

How and where I should store that token?

推荐答案

我的建议是令牌存储在本地存储,如果你使用的是HTML5。它是那样简单:

My recommendation is to store the token in local storage, if you are using HTML5. It's as simple as:

localStorage['clientToken'] = clientToken;

要通过令牌回服务器,您可以使用HTTP头和角httpInterceptor :

To pass the token back to the server, you can use a HTTP header and a Angular httpInterceptor:

$httpProvider.interceptors.push(function($q, $log, $location) {
  return {
    'request': function(config) {
      config.headers['clientToken'] = localStorage['clientToken'];
      return config;
    }
  });

一些例子可以发现这里。