also looking at this
Deploy: uniform auth session model (main → production)
#7
282 files
+3028
-64856
| @@ -0,0 +1,39 @@ | ||
| 1 | import type { RequestInstance } from '@stacksjs/types' | |
| 2 | import { Action } from '@stacksjs/actions' | |
| 3 | import { Auth } from '@stacksjs/auth' | |
| 4 | import { response } from '@stacksjs/router' | |
| 5 | import { clearAuthCookie } from './authCookie' | |
| 6 | ||
| 7 | /** | |
| 8 | * Project override of the framework's default LogoutAction — same token | |
| 9 | * revocation, plus clearing the HttpOnly `auth-token` cookie the auth actions | |
| 10 | * set (see authCookie.ts). The cookie is HttpOnly, so only the server can clear | |
| 11 | * it: the client signOut() just calls this and reloads. | |
| 12 | */ | |
| 13 | export default new Action({ | |
| 14 | name: 'LogoutAction', | |
| 15 | description: 'Logout from the application', | |
| 16 | method: 'POST', | |
| 17 | async handle(request: RequestInstance) { | |
| 18 | await Auth.logout() | |
| 19 | ||
| 20 | const clearCookie = clearAuthCookie() | |
| 21 | ||
| 22 | // A plain full-page <form method="POST"> logout is a navigation, not an | |
| 23 | // XHR, so returning JSON would render the raw payload in the browser. | |
| 24 | // Redirect browser navigations to /login; XHR/API callers (Accept: | |
| 25 | // application/json) still get JSON. | |
| 26 | const accept = String(request.headers?.get?.('accept') ?? '') | |
| 27 | if (accept.includes('text/html')) { | |
| 28 | return new Response(null, { | |
| 29 | status: 302, | |
| 30 | headers: { 'Location': '/login', 'Set-Cookie': clearCookie }, | |
| 31 | }) | |
| 32 | } | |
| 33 | ||
| 34 | return response.json( | |
| 35 | { message: 'Successfully logged out' }, | |
| 36 | { status: 200, headers: { 'Set-Cookie': clearCookie } }, | |
| 37 | ) | |
| 38 | }, | |
| 39 | }) | |