Cookie 不会通过 发送到服务器getServerSideProps
,这是前端的代码:
export async function getServerSideProps() {
const res = await axios.get("http://localhost:5000/api/auth", {withCredentials: true});
const data = await res.data;
return { props: { data } }
}
在服务器上,我有一个检查访问 JWT 令牌的策略。
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;
}
}
也就是说,当我通过getServerSideProps
cookie发送请求时,不会到达服务器,但如果我发送,例如 via useEffect
,则 cookie 会正常发送。