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

feat(server-auth): dbAuth 3/3 - handle login, logout, signup, etc. requests if forwarded from middleware #10457

Merged
merged 10 commits into from
Apr 16, 2024
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
Loading