gpt4 book ai didi

快速 session 在请求之间不持久

转载 作者:行者123 更新时间:2023-12-05 04:04:49 25 4
gpt4 key购买 nike

我在使用 express-session 时遇到问题。 session 数据不会在请求之间持续存在。

正如您在下面的代码中看到的那样,/join 路由设置了一些 session 属性,但是当 /surveys 路由被命中时, session 数据不可用。 req.session.loggedIn/surveys 路径中返回 undefined

require('dotenv').config()

const cors = require('cors')
const express = require('express')
const open = require('amqplib').connect(process.env.RABBIT_SERVER)
const session = require('express-session')
const subscribeValidator = require('./src/validators/subscribe')
const { validationResult } = require('express-validator/check')

const app = express()

app.set('trust proxy', 1)
app.use(session({
name: 'giro',
secret: process.env.SESSION_SECRET,
saveUninitialized: false,
cookie: { secure: false },
maxAge: process.env.SESSION_EXPIRY,
resave: false
}))
app.use(express.json())
app.use(cors())

app.post('/join', subscribeValidator, function (req, res) {
/*
if(session.loggedIn) {
return res.send(JSON.stringify({
redirect: '/surveys'
}))
}
*/

const data = {
firstname: req.body.firstname,
surname: req.body.surname,
email: req.body.email,
birthday: req.body.birthday,
gender: req.body.gender,
isSubscribed: req.body.isSubscribed
}

const errors = validationResult(req.body)
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() })
}

open
.then(conn => conn.createChannel())
.then(ch => ch
.assertQueue('subscribe')
.then(ok => {
ch.sendToQueue('subscribe', Buffer.from(JSON.stringify(data)))

req.session.firstname = data.firstname
req.session.email = data.email
req.session.isSubscribed = data.isSubscribed
req.session.confirmed = false
req.session.loggedIn = true

req.session.save()
res.send(JSON.stringify({
redirect: '/surveys',
firstname: data.firstname,
email: data.email
}))
})
)
.catch(console.warn)
})

app.post('/surveys', (req, res) => {
console.log(req.session.loggedIn)
if (!req.session.loggedIn) {
return res.send(JSON.stringify({
error: {
type: 'auth',
message: 'You must be logged in to view and complete surveys. Have you signed up?'
}
}))
}

res.send(JSON.stringify({
surveys: [
'one',
'two',
'three'
]
}))
})

// Start the server :)
app.listen(3000, () =>
console.log('Server running at: http://localhost:3000')
)

我检查了许多与我的问题无关的 SO 帖子,一遍又一遍地阅读文档,但我似乎仍然遗漏了一些东西。

谢谢

最佳答案

在@jfriend00 的帮助下,我解决了这个问题。因为 API 与发出请求的 CDN 位于不同的端口,所以当从 CDN 向 API 发出 xhr 请求时,它属于跨源请求。尽管我启用了 cors 以便 cookie 跨源工作,但您必须进行一些调整。

首先,我必须像这样配置 express-cors:

app.use(cors({
origin: 'http://localhost:1313',
credentials: true
}))

这会将 Access-Control-Allow-Origin header 值设置为 http://localhost:1313。不能使用通配符,否则将无法通过飞行前检查。

crednetials 属性将 Access-Control-Allow-Credentials header 值设置为 true。同样,如果没有这个,飞行前将失败。

在主机上发出请求时,我还必须使用 withCredentials。我正在使用 superagent,所以我是这样做的:

componentDidMount () {
request
.post('http://localhost:3000/surveys')
.withCredentials()
.set('accept', 'json')
.end((err, res) => {
this.setState({ isLoading: false })
...
})
}

现在可以了:)

关于快速 session 在请求之间不持久,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51976763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com