New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
req.session.get() returns undefined, but cookie is set #305
Comments
Hey there, can you provide a simple example (starting from the Next.js example of this repository) that can reproduce this issue? PS: You have to call req.session.set("user") if you want to get the user with req.session.get("user"). The fact that the cookie is named "users" and req.session.get("user") are two different things (one is cookie name, the other is serialized data inside the cookie value). |
I was reviewing my code and in my case it was a problem of the flow of my application but |
Hey there, so I am running into a similar issue. The thing that is driving me nuts is, that the cookie is set correctly in my "/login" and "/signup" routes and even req.session.destroy() works as expected on "/logout". Just req.session.get("user") returns an emty object on refresh. The cookie survives refresh though. This is my code in the "user.js" route:
Has anyone an idea? I have been struggling with this for some time now and am grateful for anything that might help. |
I've just experienced a similar issue, make sure that all your URLs are in https (or on the same protocol if you're not using the For example, I was on an |
@Bexy-Lyn I see you're using sameSite: strict. Be aware that when cookies are strict, they are not sent if you open a new tab to your website. They are only sent when you navigate (click) from yoursite.com/page1 to yoursite.com/page2. If you click on a link to yoursite.com from gmail: cookies are not sent by the browser. |
I think we've pretty much covered what can cause your session to "not be here":
If someone still has problems, open a new issue with a GitHub repository reproducing the issue, thanks! |
I'm facing the same issue. For me, the Below is my api handler – const handler = async function (req, res) {
console.log(req.session.get('user')); // returns undefined even after the session is set below
if (req.method === 'POST') {
const { email, password } = req.body;
const user = await prisma.user.findUnique({
where: {
email,
},
});
const isPasswordValid = bcrypt.compareSync(password, employer.password);
if (isPasswordValid) {
req.session.set('user', {
id: user.id,
});
await req.session.save();
console.log('saved session', req.session.get('user')); //returns saved session
res.status(200).send({});
}
res.status(500).send();
}
}; |
I have the same problem. |
I'm probably missing something here, but inside
getServerSideProps
if I callreq.session.get('user')
it returns undefined. But when I check thereq
object:req.cookies
has theuser
key with the token as its valuereq.headers.cookie
also shows theuser=abce3234
token value.What am I missing here?
My hope was to do
req.session.get('user')
and get the JWT decoded value (likeOr, if not possible, get at least the token string, so I can manually decode it.
Thank you in advance for any help.
The text was updated successfully, but these errors were encountered: