记得我在Angularjs功能和令牌 [英] Remember me functionality and Token in Angularjs

查看:226
本文介绍了记得我在Angularjs功能和令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要寻找在这里更好的方法我的问题。我有我的登录表单上记得我的功能。那些在用户点击记得我中,我的API送我令牌。

I am looking for a better approach to my problem here. I have a remember me functionality on my login form. Ones the user click on remember me box, My API sends me Token.

我的问题是什么是存储此令牌,当他们返回到我的网站重新进行身份验证的用户的最佳方式?

My question is what is the best way to store this token and authenticate user again when they are back to my site?

我想,


  1. 创建一个Cookie和存储令牌在那里。

  2. 创建一个本地存储。

请给我任何意见可能帮助我。

Please give me any advice which might help me.

推荐答案

我会使用的document.cookie有出厂code是这样的:

I would use document.cookie with a factory code like this:

创建的cookie(例如,在一年这一个超时):

Creates a cookie (for example this one expires in a year):

app.factory('$remember', function() {
    return function(name, values) {
        var cookie = name + '=';

        cookie += values + ';';

        var date = new Date();
        date.setDate(date.getDate() + 365);

        cookie += 'expires=' + date.toString() + ';';

        document.cookie = cookie;
    }
});

该工厂删除的cookie:

This factory removes the cookie:

app.factory('$forget', function() {
    return function(name) {
        var cookie = name + '=;';
        cookie += 'expires=' + (new Date()).toString() + ';';

        document.cookie = cookie;
    }
});

然后,每当用户成功使用缓存记录的关键$记住:

Then whenever a user successfully logs in cache the key using $remember:

$remember('my_cookie_name', response.user._id);

和经常检查cookie是否存在在用户登录也可以使用一个标准的登录并将其保存到自己的cookie时。如果还记得我没有启用,忘记了document.cookie

And always check if the cookie is there when logging in the user else use a a standard login and save it to their cookies. If the remember me is not enabled, forget the document.cookie

这篇关于记得我在Angularjs功能和令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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