hi, I want to get getUserId() from session object ...
# support-questions-legacy
s
hi, I want to get getUserId() from session object and i am using fastify mercurius . In documentation, there is an example for only express, so can anyone here how can I pass the session in the context in mercurius?
r
Hey!
Under nodejs tab, we do have sub tabs for other frameworks, including fastify - is that what you were looking for?
s
no, in integration tab there is graphql and in backend setup there is example for express only
r
@nkshah2 can help.
n
Right, so you want to follow the steps here: https://supertokens.com/docs/session/common-customizations/sessions/session-verification-in-api/verify-session which explains how to use the verifySession middleware (This page has docs for how to use it in fastify as well) That will give you access to the session object which you can then add to the graphlql context
s
I am using the preHandler hook to call verifySession middleware, still, I am getting session object undefined.
r
Can you show some code?
s
fastify.addHook( "preHandler", async (request: SessionRequest, reply: FastifyReply, done: () => void) => { await verifySession({ sessionRequired: false }); console.log(request.session); done(); } );
remove async await
i am just testing
r
So because you have set sessionRequired as false, it goes through even if a session does not exist
Remove that setting and then what happens? Does the frontend get a 401?
n
Like rp said since session required is false it goes through if the user isn’t logged in Since this is a GraphQL route you want to keep session required false, in which case if the session is undefined it usually means you aren’t logged in
s
but i tried by logging in
still gets undefined
r
So there could be some other reason why it's not working.. some misconfiguration
can you enable frontend and backend debug logs and show the output?
s
i currently don't have frontend app
but it works fine on REST
its problem only on graphQL
n
How are you testing it?
Also enable debug logs just on the backend in that case
s
i am just logging the req.session object
n
What recipe are you using?
s
EmailPassword
s
okay i will try that, and will let you know if anything works. Thanks for the help.😀