【发布时间】:2020-06-04 21:28:29
【问题描述】:
我对 fastify 会话有疑问。我正在使用打字稿:
import fastify from "fastify"
import randomString = require("crypto-random-string")
import fastifyCookie = require("fastify-cookie")
import fastifySession = require("fastify-session")
const app = fastify()
const safeSecret = randomString({length:32, type: 'base64'})
app.register(fastifyCookie)
app.register(fastifySession, {secret: safeSecret, saveUninitialized: true, cookie: {secure:false, httpOnly: true, sameSite: false, maxAge: 60 *60 *60}})
app.addHook('preHandler', (request, _reply, next) => {
request.session.sessionData = {userId: String, name: String, email: String, password: String, loggedOn: Date};
next();
})
app.get('/', (req, reply) => {
let oldName = req.session.sessionData.name
req.session.sessionData.name = randomString({length: 32, type: 'base64'})
reply.send("name:" + req.session.sessionData.name + " old name: " + oldName)
})
app.get('/showmename', (req, reply) => {
reply.send("name:" + req.session.sessionData.name)
})
app.listen(3000)
代码有效,但是,当我首先访问 localhost/ 时,它会显示我的随机名称,但 oldname 是下面的代码。 showmename 和 oldname 完全一样。
name:function String() { [native code] }
我做错了吗?因为当我转到 localhost/showmename 时,firefox 的 cookie 编辑器插件会向我显示与 localhost/ 具有相同会话 id 的完全相同的会话 cookie。
【问题讨论】: