ReviewOS

also looking at this

loghqorg/loghq

Deploy: uniform auth session model (main → production)

#7
Merged glennmichael123 wants to merge main into production
282 files +3028 -64856
app/Actions/Auth/SocialCallbackAction.tsmodified+16-14
Changes to app/Actions/Auth/SocialCallbackAction.ts
@@ -6,6 +6,7 @@ import { config } from '@stacksjs/config'
66import { db } from '@stacksjs/database'
77import { response } from '@stacksjs/router'
88import { GitHubProvider, GoogleProvider } from '@stacksjs/socials'
9import { buildAuthCookie, sessionExpiryMinutes } from './authCookie'
910
1011function makeDriver(provider: string): GitHubProvider | GoogleProvider | null {
1112 const svc = config.services as any
@@ -97,22 +98,23 @@ export default new Action({
9798 .execute()
9899 }
99100
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) })
101106 if (!result?.token)
102107 return fail('We could not sign you in.')
103108
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 })
117119 },
118120})