also looking at this
Deploy: uniform auth session model (main → production)
#7
282 files
+3028
-64856
| @@ -6,6 +6,7 @@ import { config } from '@stacksjs/config' | ||
| 6 | 6 | import { db } from '@stacksjs/database' |
| 7 | 7 | import { response } from '@stacksjs/router' |
| 8 | 8 | import { GitHubProvider, GoogleProvider } from '@stacksjs/socials' |
| 9 | import { buildAuthCookie, sessionExpiryMinutes } from './authCookie' | |
| 9 | 10 | |
| 10 | 11 | function makeDriver(provider: string): GitHubProvider | GoogleProvider | null { |
| 11 | 12 | const svc = config.services as any |
| @@ -97,22 +98,23 @@ export default new Action({ | ||
| 97 | 98 | .execute() |
| 98 | 99 | } |
| 99 | 100 | |
| 100 | const result = await Auth.loginUsingId(userId) | |
| 101 | // SSO has no "remember me" checkbox, so it lands on the baseline session | |
| 102 | // (a week). loginUsingId stamps expires_at from expiresInMinutes and | |
| 103 | // returns the matching expiresIn, which buildAuthCookie uses for Max-Age — | |
| 104 | // one number drives both. | |
| 105 | const result = await Auth.loginUsingId(userId, { expiresInMinutes: sessionExpiryMinutes(false) }) | |
| 101 | 106 | if (!result?.token) |
| 102 | 107 | return fail('We could not sign you in.') |
| 103 | 108 | |
| 104 | // Hand the token to the client, mirroring the email/password flow exactly: | |
| 105 | // localStorage for bearer API calls + the loghq_token cookie so the very | |
| 106 | // first server-rendered page is already authenticated. Token is embedded in | |
| 107 | // the response body, never in the URL. Then land in the app. | |
| 108 | const token = JSON.stringify(result.token) | |
| 109 | const user = JSON.stringify({ id: userId, email, name: social.name }) | |
| 110 | return response.html( | |
| 111 | `<!doctype html><meta charset="utf-8"><title>Signing you in</title>` | |
| 112 | + `<script>try{localStorage.setItem('token', ${token});localStorage.setItem('user', ${user});` | |
| 113 | + `document.cookie='loghq_token='+${token}+'; path=/; max-age=2592000; samesite=lax'+(location.protocol==='https:'?'; secure':'')}catch(e){}` | |
| 114 | + `location.replace('/dashboard')</script>Signing you in...`, | |
| 115 | 200, | |
| 116 | ) | |
| 109 | // Establish the session server-side, exactly like the email/password flow: | |
| 110 | // a 302 to /dashboard carrying the single HttpOnly `auth-token` cookie. No | |
| 111 | // localStorage, no client-readable cookie, no token in the URL. | |
| 112 | return new Response(null, { | |
| 113 | status: 302, | |
| 114 | headers: { | |
| 115 | 'Location': '/dashboard', | |
| 116 | 'Set-Cookie': buildAuthCookie(result.token, result.expiresIn), | |
| 117 | }, | |
| 118 | }) | |
| 117 | 119 | }, |
| 118 | 120 | }) |