如何在用户的信息存储记录? [英] How to store logged in user's info?

查看:120
本文介绍了如何在用户的信息存储记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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;
    }
  });

一些例子可以发现<一个href=\"http://blog.cloudme.org/2013/11/authenticating-single-page-html-applications-with-angular-js-and-flickr/\"相对=nofollow>这里。

这篇关于如何在用户的信息存储记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆