Lum*_*mca 9 javascript browser cookies fetch vercel
我有两台正在运行的 VERCEL 服务器。第一个用于服务器,第二个用于客户端。
服务器正在运行 NODE ->express ->express-sessions。
客户端正在使用 fetch api 运行 Svelte。
我的服务器正在正确设置 cookie 并发送它们,但我的浏览器没有保存 cookie。
我的 vercel.json 是:
{
"src": "/api/login",
"dest": "/api/index.js",
"headers": {
"Access-Control-Allow-Origin": "{mydomain}",
"Access-Control-Allow-Methods": "GET,OPTIONS,PATCH,DELETE,POST,PUT",
"Access-Control-Allow-Headers": "X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version",
"Access-Control-Allow-Credentials": "true"
},
"continue": true
},
Run Code Online (Sandbox Code Playgroud)
快递应用程序是:
app.set('trust proxy', 1);
app.use(cookieParser("secretcode"));
app.use(session({
secret: "secretcode",
resave: true,
saveUninitialized: true,
cookie: {
maxAge: 60000 * 10,
secure: true
},
store: new MongoStore({ mongooseConnection: mongoose.connection })
}))
app.use(passport.initialize());
app.use(passport.session());
Run Code Online (Sandbox Code Playgroud)
登录端点是:
const login = async (req, res, next) => {
passport.authenticate("local", (err, user, info) => {
if (err) throw err;
if (!user) {
res.status(200)
res.send("No User Exists")
} else {
req.logIn(user, (err) => {
if (err) throw err;
res.status(200)
req.session.user = user;
res.send({
"message": "Successfully logged in"
});
});
}
})(req, res, next)
}
Run Code Online (Sandbox Code Playgroud)
客户端代码是:
async function login() {
await fetch("{mydomain}/api/login", {
method: "post",
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
username: username,
password: password
})
})
}
Run Code Online (Sandbox Code Playgroud)
我尝试过的:
将 vercel.json Access-Control-Allow-Origin 更改为 *
添加/删除凭据:“包含”
什么都不起作用。
我确实从 https 运行所有内容,我什至只尝试过 http 但没有成功。在失眠的情况下,它也能完美地发挥作用。
| 归档时间: |
|
| 查看次数: |
1528 次 |
| 最近记录: |