-
Notifications
You must be signed in to change notification settings - Fork 468
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
Express.Session isn't obvious #13
Comments
Looks like the session type has been removed from express, you could probably just do: Request & { session: { userId: string } }; |
Thanks so much for asking this. Spent 6+ hrs trying to figure out what's happening. |
I found this to be working! in import { User } from './src/entities/User';
declare global {
namespace Express {
interface Session {
userid?: number;
}
}
} and add this to Express.Session is now available in is it okay to play with |
@dzirg44 This should work. import { Request, Response } from 'express';
import { Session, SessionData } from 'express-session';
export type MyContext = {
req: Request & {
session: Session & Partial<SessionData> & { userId?: number };
};
res: Response;
}; |
I did something like this - interface SessionData {
userId: number;
}
export type Context = {
req: Request & { session: SessionData };
}; |
Thank you, it worked |
Your videos are on the next level, thank you so much for all of this |
This solves too
|
export type MyContext = { |
I have tried several methods and for whatever reason, there is not a cookie being saved... |
@BookmDan this is all my advice for debugging cookies: https://benawad.com/cookie |
Thank you @benawad That was helpful. Actually, because we are incorporating with RedisStore would that be considered a 3rd Party Cookie? Asking for a friend. |
hey, the thing that solved the problem for me was "removing" secure while in development.
|
I have solved this by setting cors in apollo server. apolloServer.applyMiddleware({ |
According to express-session, this is what I did:
You can see where I got this idea in the Type definition for session.SessionData (ctrl+right click in VsCode):
|
I guess this issue has to be closed! like all solutions here do work fine! |
Yes I'm having a similar issue.
On to the next project for now...
…On Mon, Feb 22, 2021, 2:36 PM SSouper ***@***.***> wrote:
I literally can't get express-session to work lmao, even if it sets a
cookie, req.session is undefined and it's in the right order. Even copied
your code & package versions. It seems to send the cookie back to express
but it just refuses to fetch the set value...
At this point, setting a session cookie manually if it does not exist then
looking up the key is redis is actually a lot easier to do than getting
express-session to work.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#13 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACEDGR47SSYDDVMOZXG6MSLTALL5HANCNFSM4TRFP64A>
.
|
It seems like the apollo middleware removes every field from req that "should not be there" (session included). I put a middleware function before apollo but after express-session and req.session was there. |
do you get the same result if you clone my code? |
@benawad looks like i forgot to destructure the first argument to the context function. Strangely, typescript didn't tell me that i'm a bad boy... |
Trying this method has resulted in this error for me
Package.json
The solution I found was adding this, but it feels kind of hacky, is there a better solution?
|
It should be
because again in UserResolver me( ) Query, Id is a number.
|
maybe you should check your constant |
I found a weird behaviour and can't really explain why this is: When setting the So for example the following code req.session.userId = user._id;
console.log(req.session) // this gets printed
return { user }; would print the session object with cookie and userId, but the frontend never leaves the fetching state and eventually times out. The solution is an incompatibility between connect-redis and node-redis as described in this issue: Replace this line in index.ts |
This post helps fix the the switch from playground to sandbox if you are unable to test the cookie login https://stackoverflow.com/questions/69333408/express-session-does-not-set-cookie/70673115#70673115 |
Hi @benawad ! Thank you for your great videos.
Today I faced with one problem here
lireddit/server/src/types.ts
Line 7 in 2801126
as I can see this type we get from
express-session
even though you tried to import it fromexpress
and it didn't work.when I try to write something like
Express.Session
I get the errori tried to solve it via, but I am not good at typescript ;)
in
server/src/types.ts
and
in the
server/src/resolvers/user.ts
versions:
sorry in advance if I just didn't understand or miss something .
The text was updated successfully, but these errors were encountered: