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/RegisterAction.tsadded+78-0
Changes to app/Actions/Auth/RegisterAction.ts
@@ -0,0 +1,78 @@
1import type { RequestInstance } from '@stacksjs/types'
2import { Action } from '@stacksjs/actions'
3import { Auth, register } from '@stacksjs/auth'
4import { response } from '@stacksjs/router'
5import { schema } from '@stacksjs/validation'
6import { buildAuthCookie } from './authCookie'
7
8/**
9 * Account creation, registered at POST /api/auth/register (routes/auth.ts).
10 *
11 * Replaces the old register.stx page action. Like LoginAction, on success the
12 * issued bearer is mirrored into the HttpOnly `auth-token` cookie so the very
13 * first server-rendered page after signup is already authenticated. loghq has
14 * no teams model, so there is no personal-team creation here — just the account
15 * and its session. Where the user lands afterwards (an invited user goes to the
16 * dashboard, everyone else to the create-your-first-app page) is decided by the
17 * client, which knows the invite context from the URL.
18 */
19export default new Action({
20 name: 'RegisterAction',
21 description: 'Register a new user',
22 method: 'POST',
23
24 validations: {
25 name: {
26 rule: schema.string().min(2).max(255),
27 message: 'Name must be between 2 and 255 characters.',
28 },
29 email: {
30 rule: schema.string().email(),
31 message: 'Email must be a valid email address.',
32 },
33 password: {
34 rule: schema.string().min(8).max(255),
35 message: 'Password must be at least 8 characters.',
36 },
37 },
38
39 async handle(request: RequestInstance) {
40 const name = String(request.get('name') ?? '').trim()
41 const email = String(request.get('email') ?? '').trim()
42 const password = String(request.get('password') ?? '')
43
44 // `register` throws an HttpError for an invalid address, a short password or
45 // a duplicate. Its duplicate message is deliberately vague — the framework
46 // treats "this email exists" as an enumeration oracle — so surface it as-is.
47 let result
48 try {
49 result = await register({ name, email, password })
50 }
51 catch (err) {
52 return response.json(
53 { message: (err as Error)?.message || 'Could not create the account.' },
54 { status: 422 },
55 )
56 }
57
58 if (!result?.token)
59 return response.json({ message: 'Could not create the account.' }, { status: 422 })
60
61 // register() returns the token pack but not the user record; resolve it for
62 // the response body. The cookie's Max-Age matches the token's own expiry
63 // (config baseline — a week), so signup lands on the baseline tier.
64 const user = await Auth.getUserFromToken(result.token)
65
66 return response.json(
67 {
68 token: result.token,
69 user: {
70 id: user?.id,
71 email: user?.email,
72 name: user?.name,
73 },
74 },
75 { status: 200, headers: { 'Set-Cookie': buildAuthCookie(result.token, result.expiresIn) } },
76 )
77 },
78})