Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example on how to perform authorization when using subscriptions #1694

Merged
merged 1 commit into from
Aug 26, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions content/graphql/subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,40 @@ GraphQLModule.forRoot({
}
}),
```

#### Authorization over WebSocket

Checking that the user is authenticated should be done inside the `onConnect` property of the `subscriptions` options (read [more](https://www.apollographql.com/docs/graphql-subscriptions/authentication/)).
The `onConnect` will receive as first argument the `connectionParams` passed to the `SubscriptionClient` (read [more](https://www.apollographql.com/docs/react/data/subscriptions/#4-authenticate-over-websocket-optional)).

```typescript
GraphQLModule.forRoot({
installSubscriptionHandlers: true,
subscriptions: {
onConnect: (connectionParams) => {
// extract the token
const authToken = connectionParams.authToken;
// validate the token (e.g., signature, expiration for jwt)
if (!isValid(authToken)) {
throw new Error('Token is not valid');
}
// extract user information from token
const user = parseToken(authToken);
// return user info to add them to the context later
return { user };
},
},
context: ({ connection }) => {
// connection.context will be equal to what was returned by onConnect
// now user info is available inside context.req.user
return {
req: connection?.context ?? {},
};
},
}),
```

The `authToken` in this example is only sent once by the client, when the connection is first established.
All subscriptions made with this connection will have the same `authToken`, and thus the same user info.

> warning **Note** There is a bug in `subscriptions-transport-ws` that allows connections to skip the `onConnect` phase (read [more](https://github.com/apollographql/subscriptions-transport-ws/issues/349)). You should not assume that `onConnect` was called when the user starts a subscription, and always check that the `context` is populated.