Skip to content

Commit

Permalink
feat: simplify done() function (#647)
Browse files Browse the repository at this point in the history
  • Loading branch information
ankur0904 authored Dec 20, 2023
1 parent 0255757 commit c6fade7
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
4 changes: 1 addition & 3 deletions docs/pages/authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,8 @@ export async function serverAuth({ authProps, done }) {
**Parameters for done():**

- Authentication Result (Boolean): true for success, false for failure.
- HTTP Status Code (Integer): Code for authentication failure (e.g., 401 for Unauthorized).
- Status Message (String): Description of the authentication result (e.g., "Unauthorized").

When `true` is passed to the done parameter, the server/broker knows to go ahead and allow the client to connect, which means authentication has succeeded. However if the `done` parameter is called with `false` then the server knows to throw an error message and reject the client, which means authenticatio has failed.
When `true` is passed to the done parameter, the server/broker knows to go ahead and allow the client to connect, which means authentication has succeeded. However if the `done` parameter is called with `false` then the server knows to throw an error message and reject the client, which means authentication has failed.

`done()` should always be the last thing called in a `serverAuth` function, Glee won't execute any logic beyond the `done()` call.

Expand Down
4 changes: 2 additions & 2 deletions src/adapters/http/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ class HttpAdapter extends Adapter {
})
return {
promise,
done: (val: boolean, code = 401, message = 'Unauthorized') => {
done: (val: boolean) => {
if (val) {
resolveFunc(true)
} else {
rejectFunc({ code, message })
rejectFunc({ code: 401, message: 'Unauthorized' })
}
},
}
Expand Down
6 changes: 3 additions & 3 deletions src/adapters/ws/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,10 +218,10 @@ class WebSocketsAdapter extends Adapter {
}

private _wrapCallbackDecorator(cb) {
return function done(val: boolean, code = 401, message = 'Unauthorized') {
cb(val, code, message)
return function done(val: boolean) {
cb(val)
if (val === false) {
const err = new Error(`${code} ${message}`)
const err = new Error("401, Unauthorized")
this.emit('error', err)
}
}
Expand Down

0 comments on commit c6fade7

Please sign in to comment.