我有一個名為 login 的 GQL 突變,它在回應中回傳一個 JWT 作為 cookie。
盡管存在Set-Cookie標題,但我的本地存盤選項卡中沒有任何內容。我的控制臺中也沒有列印任何內容......
這是我的登錄突變
@Mutation((returns) => AuthenticatedUser)
async login(
@Context() context: Auth.GqlContext,
@Args('payload', { type: () => UserLoginDto }) payload: UserLoginDto
): Promise<AuthenticatedUser> {
const authenticatedUser = await this.authService.login(payload)
context.res.cookie('jwt', authenticatedUser.jwt, {
httpOnly: true,
secure: process.env.NODE_ENV === 'production',
sameSite: 'lax',
maxAge: 1000 * 60 * 60 * 24 * 31, // one month
domain: process.env.NODE_ENV === 'production' ? '.herbievine.com' : undefined
})
return authenticatedUser
}
作為 JSON 的示例回應:
{
"data": {
"login": {
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjI0N2QyNmY3LTM3NzQtNGRiNS1hM2MzLTE2MTg3Mzk2ODdlMCIsImVtYWlsIjoidmluZWhlcmJpZUBnbWFpbC5jb20iLCJpYXQiOjE2MzYxMjkyMTksImV4cCI6MTYzODcyMTIxOX0.hHHAAgzs7wFjMP2C565fGGHAd0o-C9h-jA5qzm48OdE",
"user": {
"email": "[email protected]",
"name": "Some Name"
}
}
}
}
回復:
HTTP/1.1 200 OK
X-Powered-By: Express
Access-Control-Allow-Origin: http://localhost:4000
Vary: Origin
Access-Control-Allow-Credentials: true
Set-Cookie: jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjI0N2QyNmY3LTM3NzQtNGRiNS1hM2MzLTE2MTg3Mzk2ODdlMCIsImVtYWlsIjoidmluZWhlcmJpZUBnbWFpbC5jb20iLCJpYXQiOjE2MzYxMjkyMTksImV4cCI6MTYzODcyMTIxOX0.hHHAAgzs7wFjMP2C565fGGHAd0o-C9h-jA5qzm48OdE; Max-Age=2678400; Path=/; Expires=Mon, 06 Dec 2021 16:06:11 GMT; HttpOnly; SameSite=Lax
Content-Type: application/json; charset=utf-8
Content-Length: 319
ETag: W/"13f-MuV4uipvL8ui1YJXuTSvJ1TM1H0"
Date: Fri, 05 Nov 2021 16:06:11 GMT
Connection: keep-alive
Keep-Alive: timeout=5
要求:
POST /graphql HTTP/1.1
Host: localhost:3000
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:78.0) Gecko/20100101 Firefox/78.0
Accept: */*
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Referer: http://localhost:3000/graphql
content-type: application/json
Origin: http://localhost:3000
Content-Length: 195
Connection: keep-alive
目前,我還沒有設定我的前端代碼庫,它將設定為 http://localhost:4000。這一切都在 GraphQL 游樂場中進行測驗
更新
gql 游樂場的配置:
{
"editor.cursorShape": "line",
"editor.fontFamily": "'Source Code Pro', 'Consolas', 'Inconsolata', 'Droid Sans Mono', 'Monaco', monospace",
"editor.fontSize": 14,
"editor.reuseHeaders": true,
"editor.theme": "dark",
"general.betaUpdates": false,
"prettier.printWidth": 80,
"prettier.tabWidth": 2,
"prettier.useTabs": false,
"request.credentials": "omit",
"schema.disableComments": true,
"schema.polling.enable": true,
"schema.polling.endpointFilter": "*localhost*",
"schema.polling.interval": 2000,
"tracing.hideTracingResponse": true,
"queryPlan.hideQueryPlanResponse": true
}
應用模塊:
@Module({
imports: [
GraphQLModule.forRoot({
autoSchemaFile: join(process.cwd(), 'schema.gql'),
context: ({ req, res }) => ({ req, res }),
cors: {
origin:
process.env.NODE_ENV === 'production'
? '.herbievine.com'
: 'http://localhost:4000',
credentials: true,
}
}),
ThrottlerModule.forRoot({
ttl: 60,
limit: 20
}),
PostsModule,
CategoriesModule,
UsersModule,
AuthModule
],
controllers: [AppController],
providers: [AppService, PrismaService]
})
export class AppModule {}
認證模塊:
@Module({
imports: [
PassportModule.register({ defaultStrategy: 'jwt' }),
JwtModule.register({
secret: process.env.JWT_SECRET,
signOptions: {
expiresIn: '30d'
}
})
],
providers: [
PrismaService,
JwtStrategy,
LocalStrategy,
AuthService,
UsersService,
AuthResolver
],
controllers: [AuthController],
exports: [AuthService]
})
export class AuthModule {}
uj5u.com熱心網友回復:
默認情況下,GQL 游樂場不發送憑據。在 GQL 游樂場的選項中,設定request.credentials = 'include'它應該可以作業
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/349490.html
