Skip to content

Commit 8047b19

Browse files
authored
Passport-Local: GraphQL Guard
1 parent 20b1b96 commit 8047b19

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

content/recipes/passport.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -943,7 +943,8 @@ 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.
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.
947948
948949
```typescript
949950
@Injectable()
@@ -955,6 +956,22 @@ export class GqlAuthGuard extends AuthGuard('jwt') {
955956
}
956957
```
957958
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+
958975
To get the current authenticated user in your graphql resolver, you can define a `@CurrentUser()` decorator:
959976
960977
```typescript

0 commit comments

Comments
 (0)