gpt4 book ai didi

typescript - 如何从 NestJS 中的 GraphQL 解析器内部设置 session key ?

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

我正在发现 Nest.js 并且我想使用 GraphQL 设置一个基于 cookie 的身份验证系统。

我已经安装了 express-session 中间件,这里是配置:

main.ts

 app.use(
session({
store: new redisStore({
client: redis
} as any),
name: 'qid',
secret: SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: {
httpOnly: true,
secure: !isDev,
maxAge: 1000 * 60 * 60 * 24 * 7 * 365
}
})
)

它工作正常,因为当我这样做时:
 app.use((req: any, res: any, next: any) => {
// Debug purpose
req.session.userId = '42'
next()
})

添加了 cookie。

现在我有两个突变, 注册 登录 .
在登录突变中(或在 userService 中),在我找到一个用户之后,我想做一些类似 req.session.userId = user.id 的事情。但我找不到办法做到这一点。

我尝试添加 @Context() ctx到我的突变。
如果我控制台 log ctx,它包含我期望的所有内容(例如 req.session.id)

但如果我这样做 ctx.req.session.userId = 'something' ,cookie没有设置!

这是我的突变:

user.resolver.ts
@Mutation('login')
async login(
@Args('email') email: string,
@Args('password') password: string,
@Context() ctx: any
) {
console.log(ctx.req.session.id) // Show the actual session id
ctx.req.session.userId = 'something' // Do not set any cookie
return await this.userService.login(email, password)
}
}

我完全迷失了,我真的需要帮助,我很想了解发生了什么。我知道我这样做可能完全错误,但我对 Nest 和 GraphQL 都不熟悉。

谢谢你们...

最佳答案

很多天后我发现了这个问题......它是......

GraphQL Playground -> Settings -> 添加“request.credentials”:“include”(并且没有“omit”)

希望它会帮助某人..

关于typescript - 如何从 NestJS 中的 GraphQL 解析器内部设置 session key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55899674/

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