Skip to content

Commit 9674719

Browse files
Merge branch 'lauticouget-patch-1'
2 parents 6c06496 + 4eb5972 commit 9674719

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

content/recipes/passport.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,7 @@ Then, you refer to this via a decorator like `@UseGuards(AuthGuard('myjwt'))`.
943943
944944
#### GraphQL
945945
946-
In order to use an AuthGuard with [GraphQL](https://docs.nestjs.com/graphql/quick-start), extend the built-in AuthGuard class and override the getRequest() method.
946+
In order to use an AuthGuard with [GraphQL](https://docs.nestjs.com/graphql/quick-start), extend the built-in `AuthGuard` class and override the `getRequest()` method.
947947
948948
```typescript
949949
@Injectable()
@@ -978,3 +978,19 @@ whoAmI(@CurrentUser() user: User) {
978978
return this.usersService.findById(user.id);
979979
}
980980
```
981+
982+
For the passport-local strategy, you'll also need to add the GraphQL context's arguments to the request body so Passport can access them for validation. Otherwise, you'll get an Unauthorized error.
983+
984+
```typescript
985+
@Injectable()
986+
export class GqlLocalAuthGuard extends AuthGuard('local') {
987+
getRequest(context: ExecutionContext) {
988+
const gqlExecutionContext = GqlExecutionContext.create(context);
989+
const gqlContext = gqlExecutionContext.getContext();
990+
const gqlArgs = gqlExecutionContext.getArgs();
991+
992+
gqlContext.req.body = { ...gqlContext.req.body, ...gqlArgs };
993+
return gqlContext.req;
994+
}
995+
}
996+
```

0 commit comments

Comments
 (0)