Hello,
A lot of documentation shows that we can get the authorization token from the header inside the context function like :
context: async ({ req, res }) => {
const token = req.headers.authorization || ‘’;
const user = await getUser(token);
return { user };
},
I want to do the same but using the cookie such as :
context: async ({ req, res }) => {
const cookie = req.headers.cookie || ‘’;
const user = await getUser(cookie[“token”]);
return { user };
},
But the context function does not include any cookie information, i always get undefined.
I have tried different attribute nothing works :
const cookie = req.headers.cookie || ‘’;
…
const cookie = req.cookies || ‘’;
I have create a middleware that can get the cookie without issues but when it comes to the context function the cookie disappear from the req variable.
Do you know why i can not access the cookie from the context function?
How can I pass the cookie value inside the context?
Do I have to use the middleware to extract the cookie and add the value of the cookie to the req.headers.authorization key and then use the req.headers.authorization inside my context function?
Is it the right way to do it?
Here is my code
import cookieParser from ‘cookie-parser’
…
const testmiddleware=(req,res,next)=>{
console.log(req.cookies) # This is working
res.cookie(‘auth’,102, { maxAge: 86400 * 2, httpOnly: true, secure: true, sameSite : ‘None’ });
next()
}
…app.use(
‘/’,
cookieParser(),
json(),
cors(corsOptions),
testmiddleware,
expressMiddleware(server, {
context: async ({ req,res,t }) => {
console.log(req.cookies) #undefined
console.log(req.headers.cookies) #undefined
},
}),
);
Thank you for your help