Skip to content

Commit

Permalink
docs: recommend auth in onConnect [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed May 2, 2022
1 parent 84e61da commit 45d1649
Showing 1 changed file with 2 additions and 11 deletions.
13 changes: 2 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1018,17 +1018,13 @@ function handleAuth(request: http.IncomingMessage) {
const gqlServer = makeServer<Extra>({
schema,
onConnect: async (ctx) => {
// do your auth on every connect
// do your auth on every connect (recommended)
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);
},
});

// create websocket server
Expand Down Expand Up @@ -1713,7 +1709,7 @@ useServer(
{
schema,
onConnect: async (ctx) => {
// do your auth check on every connect
// do your auth check on every connect (recommended)
if (!(await isTokenValid(ctx.connectionParams?.token)))
// returning false from the onConnect callback will close with `4403: Forbidden`;
// therefore, being synonymous to ctx.extra.socket.close(4403, 'Forbidden');
Expand All @@ -1724,11 +1720,6 @@ useServer(
if (!(await isTokenValid(ctx.connectionParams?.token)))
return ctx.extra.socket.close(CloseCode.Forbidden, 'Forbidden');
},
onNext: async (ctx) => {
// why not on every result emission? lol
if (!(await isTokenValid(ctx.connectionParams?.token)))
return ctx.extra.socket.close(CloseCode.Forbidden, 'Forbidden');
},
},
wsServer,
);
Expand Down

0 comments on commit 45d1649

Please sign in to comment.