Skip to content

Commit

Permalink
fix(dbauth-mw): Unset cookie instead of clearing (#10502)
Browse files Browse the repository at this point in the history
  • Loading branch information
dac09 authored Apr 26, 2024
1 parent 9d482be commit 5db6c18
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .changesets/10502.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- fix(dbauth-mw): Unset cookie instead of clearing (#10502) by @dac09
Updates dbAuth middleware implementation to _unset_ the cookies, instead of clearing them.
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,12 @@ describe('createDbAuthMiddleware()', () => {

const serverAuthContext = req.serverAuthContext.get()
expect(serverAuthContext).toBeNull()

expect(res.toResponse().headers.getSetCookie()).toEqual([
// Expired cookies, will be removed by browser
'session_8911=; Expires=Thu, 01 Jan 1970 00:00:00 GMT',
'auth-provider=; Expires=Thu, 01 Jan 1970 00:00:00 GMT',
])
})
it('handles a GET request with no cookies', async () => {
const request = new Request('http://localhost:8911/functions/no-cookie', {
Expand Down
7 changes: 4 additions & 3 deletions packages/auth-providers/dbAuth/middleware/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ export const createDbAuthMiddleware = ({
console.error(e, 'Error decrypting dbAuth cookie')
req.serverAuthContext.set(null)

// Clear the cookies, because decryption was invalid
res.cookies.clear(cookieNameCreator(cookieName))
res.cookies.clear('auth-provider')
// Note we have to use ".unset" and not ".clear"
// because we want to remove these cookies from the browser
res.cookies.unset(cookieNameCreator(cookieName))
res.cookies.unset('auth-provider')
}

return res
Expand Down

0 comments on commit 5db6c18

Please sign in to comment.