Skip to content

Commit 4eb5972

Browse files
chore: improve wording
1 parent ba1ee31 commit 4eb5972

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

content/recipes/passport.md

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -943,8 +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.
947-
In the case of passport-local strategy you will also have to attach the GraphQL context's arguments into the request's body so that Passport can use them for validation.
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.
948947
949948
```typescript
950949
@Injectable()
@@ -956,22 +955,6 @@ export class GqlAuthGuard extends AuthGuard('jwt') {
956955
}
957956
```
958957
959-
In the case of passport-local strategy you will also have to attach the GraphQL context's arguments into the request's body so that Passport can access them for validation (if not done so an Unauthorized error gets issued).
960-
961-
```typescript
962-
@Injectable()
963-
export class GqlLocalAuthGuard extends AuthGuard('local') {
964-
getRequest(context: ExecutionContext) {
965-
const gqlExecutionContext = GqlExecutionContext.create(context);
966-
const gqlContext = gqlExecutionContext.getContext();
967-
const gqlCArgs = gqlExecutionContext.getArgs();
968-
969-
gqlContext.req.body = { ...gqlContext.req.body, ...gqlArgs };
970-
return gqlContext.req;
971-
}
972-
}
973-
```
974-
975958
To get the current authenticated user in your graphql resolver, you can define a `@CurrentUser()` decorator:
976959
977960
```typescript
@@ -995,3 +978,19 @@ whoAmI(@CurrentUser() user: User) {
995978
return this.usersService.findById(user.id);
996979
}
997980
```
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)