节点js中的持久性cookie表示 [英] Persistent cookies in node js express

查看:165
本文介绍了节点js中的持久性cookie表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现记住我的用户在我的express js代码。在这个如果用户检查记住我用户,那么我们需要记住用户凭证14天。

I am trying to implement remember me user in my express js code. In this if user check the remember me user then we need to remember the user credential for 14 days.

我在Cookie中设置凭据详细信息,并尝试获取凭据

I have set credential details in cookie and tried to fetch the credential from cookie for the next visit.

//set cookie value
res.cookie('email', emailId);

//retrieve cookie value
req.cookies.email;

如果我关闭浏览器并再次打开它,那么这些cookie值不会持久,

If I close the browser and open it again then these cookie values are not persisting and I am getting credential as undefined.

我们使用了以下模块:

var express      = require('express')
var cookieParser = require('cookie-parser')
var app = express()
app.use(cookieParser())

请让我知道如何在重新启动浏览器时保留Cookie值。

Please let me know how to persist cookie value on restarting browser.

感谢,
- Sandy

Thanks, -- Sandy

推荐答案

需要研究的两件事:

首先,你设置cookie过期了吗?尝试设置为:

First, are you setting the cookie expiration? Try setting to:

res.cookie('email', emailId, { maxAge: 900000, httpOnly: true });

其次,确保在浏览器关闭时不清除Cookie等清除矿井)。

Secondly, make sure that you aren't clearing cookies, etc. when the browser is closed (I clear mine).

这篇关于节点js中的持久性cookie表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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