我的会话cookie被阻止,网络选项卡显示以下内容:image of the cookie in the network tab
’这个问题我已经被困了三天了,我不知道该怎么办
我试着把这个放在cookie会话配置中,但它似乎没有做任何事情
Index.js
const cookieSession = require('cookie-session');
const cors = require('cors');
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const listingRoutes = require('./routes/listingRoutes');
const userRoutes = require('./routes/userRoutes');
const authRoutes = require('./routes/authRoutes');
const app = express();
const PORT = process.env.PORT;
const passport = require('passport')
const passportCfg = require('./passport')
app.use(cors({
origin: [process.env.FRONTEND_URL, process.env.BACKEND_URL],
methods: 'GET,POST,PUT,DELETE,PATCH',
credentials: true
}));
app.use(cookieSession({
name: 'session',
keys: ['weerit'],
maxAge: 24*60*60*1000,
sameSite: 'none',
secure: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.json({ limit: '100mb' }));
// Routes
app.use('/auth', authRoutes);
app.use('/api/users', userRoutes);
app.use('/api/listings', listingRoutes);
mongoose.connect(process.env.MONGO_URI)
.then(() => {
// Listen for requests
app.listen(PORT, () => {
console.log(`Connected to db, listening on port ${PORT}`);
});
})
.catch((err) => {
console.log(err);
});
这是一个API端点,当我获取它时不工作,但当我在url栏中输入它时工作:
//res.setHeader('X-Content-Type-Options', 'nosniff');
if(req.user){
res.status(200).json({
success: true,
message: "success",
user: req.user,
cookies: req.cookies
})
}else{
res.status(403).json({ message: "Auth has Failed" })
}
})```
1条答案
按热度按时间brc7rcf01#
我解决了它哦我的好花了我3天,这是一行代码,挽救了我的心理健康:
app.set('trust proxy', 1)
,我把它放在cookieSession中间件之前