also looking at this
Deploy: uniform auth session model (main → production)
#7
282 files
+3028
-64856
| @@ -17,6 +17,18 @@ export default new Middleware({ | ||
| 17 | 17 | if (!isValid) |
| 18 | 18 | throw new HttpError(401, 'Unauthorized. Invalid token.') |
| 19 | 19 | |
| 20 | // Stamp the resolved user like the cookie branch below and the | |
| 21 | // framework-default middleware do — `request.user()` returns | |
| 22 | // `_authenticatedUser` with no fallback, so without this a route behind | |
| 23 | // `.middleware('auth')` that reads `await request.user()` (e.g. | |
| 24 | // CreateCheckoutAction) saw undefined even after a valid bearer passed | |
| 25 | // here. Do NOT overwrite `request.user` itself — it is the accessor. | |
| 26 | const user = await Auth.getUserFromToken(bearerToken) | |
| 27 | if (user) { | |
| 28 | Auth.setUser(user) | |
| 29 | ;(request as { _authenticatedUser?: unknown })._authenticatedUser = user | |
| 30 | } | |
| 31 | ||
| 20 | 32 | log.debug(`[middleware:auth] Bearer token valid`) |
| 21 | 33 | return |
| 22 | 34 | } |
| @@ -51,11 +63,18 @@ export default new Middleware({ | ||
| 51 | 63 | |
| 52 | 64 | if (sessionId) { |
| 53 | 65 | log.debug(`[middleware:auth] Validating session`) |
| 54 | const { sessionCheck } = await import('@stacksjs/auth') | |
| 66 | const { sessionCheck, sessionUser } = await import('@stacksjs/auth') | |
| 55 | 67 | const isValid = await sessionCheck(sessionId) |
| 56 | 68 | if (!isValid) |
| 57 | 69 | throw new HttpError(401, 'Unauthorized. Session expired.') |
| 58 | 70 | |
| 71 | // Same stamping for the session branch. | |
| 72 | const user = await sessionUser(sessionId) | |
| 73 | if (user) { | |
| 74 | Auth.setUser(user) | |
| 75 | ;(request as { _authenticatedUser?: unknown })._authenticatedUser = user | |
| 76 | } | |
| 77 | ||
| 59 | 78 | log.debug(`[middleware:auth] Session valid`) |
| 60 | 79 | return |
| 61 | 80 | } |