Skip to content

Commit

Permalink
feat(server-auth): dbAuth 3/3 - handle login, logout, signup, etc. re…
Browse files Browse the repository at this point in the history
…quests if forwarded from middleware (#10457)

Co-authored-by: David Thyresson <dthyresson@gmail.com>
  • Loading branch information
dac09 and dthyresson authored Apr 16, 2024
1 parent bd82da2 commit dca417f
Show file tree
Hide file tree
Showing 6 changed files with 395 additions and 199 deletions.
28 changes: 28 additions & 0 deletions .changesets/10457.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
- feat(server-auth): dbAuth 3/3 - handle login, logout, signup, etc. requests if forwarded from middleware (#10457) by @dac09

This PR updates the DbAuthHandler class to handle requests forwarded from middleware, so it can generate responses for login, logout, signup, etc. These are POST requests - it used to be to the `/auth` function, but now they will be captured by dbAuth middleware and forwarded onto DbAuthHandler.

**High level changes:**
- use the `Headers` class in each of the "method" responses. This allows us to set multi-value headers like Set-Cookie. A simple object would not. See type `AuthMethodOutput`
- extracts `buildResponse` into a testable function and adds test. For `Set-Cookie` headers we return an array of strings.

In the middleware here's the code I had for the final conversion:
```ts
if (AUTHHANDLER_REQUEST) {
const output = await dbAuthHandler(req)

const finalHeaders = new Headers()
Object.entries(output.headers).forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach((v) => finalHeaders.append(key, v))
} else {
finalHeaders.append(key, value)
}
})

return new MiddlewareResponse(output.body, {
headers: finalHeaders,
status: output.statusCode,
})
}
```
Loading

0 comments on commit dca417f

Please sign in to comment.