[英]How can I set cookies in response object in a NestJS GraphQL application?
[英]How can I set a session key from inside a GraphQL resolver in NestJS?
我發現Nest.js,並且想使用GraphQL設置基於cookie的身份驗證系統。
我已經安裝了快速會話中間件,這是配置:
主要
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
添加到我的突變中。 如果我使用控制台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->設置->添加“ request.credentials”:“ include”(不包括“忽略”)
希望對別人有幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.