Cil*_*ilp 7 javascript authentication cookies http cross-domain
我的应用程序由 API (nestjs) 和客户端 (nextjs) 组成。
我正在使用 getServerSideProps 来验证用户身份,并在发生错误时重定向到登录站点:
export const getServerSideProps: GetServerSideProps = async (context) => {
try {
// Check whether the user is logged in
await axios.get(
`${process.env.NEXT_PUBLIC_API}/authentication`,
{
withCredentials: true,
// Cookies are attached to the request with context
headers: context.req ? { cookie: context.req.headers?.cookie || '' } : { cookie: '' },
},
);
return {
props: {},
};
} catch (err) {
return {
redirect: {
destination: '/login',
permanent: false,
},
};
}
};
Run Code Online (Sandbox Code Playgroud)
设置cookie的服务器端代码:
public getCookieWithJwtToken(userId: number) {
const payload: TokenPayload = { userId };
const token = this.jwtService.sign(payload);
return `Authentication=${token}; HttpOnly; Path=/; Secure; SameSite=None; Max-Age=${this.configService.get(
'JWT_EXPIRATION_TIME',
)}`;
}
Run Code Online (Sandbox Code Playgroud)
Cookie 在开发模式下运行良好。我能够登录、使用经过身份验证的用户的实用程序并注销。但是,当我将服务器部署到 vercel 时,cookie 不会保留在“应用程序 --> Cookies”选项卡中。
Cookie 会显示在 Cookie 响应中,但不会应用到浏览器中。
生产中的cookie信息:
Cookie 按开发中的预期工作:
到目前为止,我已经在这个问题上苦苦挣扎了几个小时,非常感谢您的帮助。