Skip to content

Commit

Permalink
docs(recipe): uWebSockets server usage with custom auth handling
Browse files Browse the repository at this point in the history
  • Loading branch information
maxpain committed Jun 1, 2021
1 parent f9caa6f commit f97cfb4
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,57 @@ wsServer.on('connection', (socket, request) => {

</details>

<details id="uws-auth-handling">
<summary><a href="#uws-auth-handling">🔗</a> Server usage with <a href="https://github.com/uNetworking/uWebSockets.js">uWebSockets.js</a> and custom auth handling</summary>

```typescript
import uWS from 'uWebSockets.js'; // yarn add uWebSockets.js@uNetworking/uWebSockets.js#<tag>
import cookie from 'cookie';
import { makeBehavior, Request } from 'graphql-ws/lib/use/uWebSockets';
import { schema } from './my-graphql-schema';
import { validate } from './my-auth';

// your custom auth
class Forbidden extends Error {}
function handleAuth(request: Request) {
// do your auth on every subscription connect
const good = validate(request.headers['authorization']);
// or const { iDontApprove } = session(cookie.parse(request.headers.cookie));
if (!good) {
// throw a custom error to be handled
throw new Forbidden(':(');
}
}

uWS
.App()
.ws(
'/graphql',
makeBehavior({
schema,
onConnect: async (ctx) => {
// do your auth on every connect
await handleAuth(ctx.extra.request);
},
onSubscribe: async (ctx) => {
// or maybe on every subscribe
await handleAuth(ctx.extra.request);
},
onNext: async (ctx) => {
// haha why not on every result emission?
await handleAuth(ctx.extra.request);
},
}),
)
.listen(4000, (listenSocket) => {
if (listenSocket) {
console.log('Listening to port 4000');
}
});
```

</details>

<details id="express">
<summary><a href="#express">🔗</a> <a href="https://github.com/websockets/ws">ws</a> server usage with <a href="https://github.com/graphql/express-graphql">Express GraphQL</a></summary>

Expand Down

0 comments on commit f97cfb4

Please sign in to comment.