Cookies are not sent to the server via getServerSideProps
, here is the code in the front-end:
export async function getServerSideProps() {
const res = await axios.get("http://localhost:5000/api/auth", {withCredentials: true});
const data = await res.data;
return { props: { data } }
}
On the server I have a strategy that checks the access JWT token.
export class JwtStrategy extends PassportStrategy(Strategy, "jwt") {
constructor() {
super({
ignoreExpiration: false,
secretOrKey: "secret",
jwtFromRequest: ExtractJwt.fromExtractors([
(request: Request) => {
console.log(request.cookies) // [Object: null prototype] {}
let data = request.cookies['access'];
return data;
}
]),
});
}
async validate(payload: any){
return payload;
}
}
That is, when I send a request via getServerSideProps
cookies do not come to the server, although if I send, for example via useEffect
, then cookies come normally.
getServerSideProps
get a ctx argument which has req, and res, refer here , you can use nookies library to parse the cookies – BriantgetServerSideProps
context to theaxios
request. If you tried that and it's still not working can you show us how you're doing it? – SchillergetServerSideProps()
does not set cookies in the browser too. I understand why cookies are not being passed to the server, but I cannot get why they are not set from the server to the frontend – Kelter