Repository navigation
Description
Why
Two people can both look brand new the first time they sign in with Google, and they need opposite treatment:
| Member: first time in an existing workspace | Founder: first time, with no workspace yet | |
|---|---|---|
| What's true | A workspace exists, and an admin has invited this email with a role | No workspace exists for them, and no invite matches their verified email |
| Must NOT see | "Create a workspace". That would split their company into a duplicate tenant. | A dead end like "no workspace, ask an admin" when signup is on |
| Goes to | /join, then the member tour |
/welcome, then the founder tour |
This ticket is the single decision point that sorts them. Getting it wrong is costly:
- A member who gets the founder flow creates a stray tenant, and their colleagues can't see their work.
- A founder who gets the member flow gets stuck.
What the user sees
- A member with a pending invite sees
/join: "You're joining Acme as steward, invited by Dana". They click Join and land in Acme.- This works even if they never received the invite link. Production can't send mail today, so an admin can simply add their email and say "sign in with Google".
- A member with several pending invites picks which workspaces to join.
- A member whose invite expired sees "Your invite to Acme expired — ask Dana to resend it". A smaller secondary link offers "Create my own workspace instead".
- A person who already has a password account with this email sees "This email has a password account — sign in with it, then link Google in Settings → Profile."
- Nobody matches (a founder):
/welcome. If self-serve signup is turned off, they see "Ask your admin for an invite" instead.
How
-
Create one server-only module, the first-sign-in router. It is called only from the Google callback (Sign in with Google: the core OIDC flow (start, callback, linking, mock IdP) #1399), with that callback's verified claims and an optional invite token.
-
It runs these checks in this order and stops at the first match:
- Identity found. The callback already handled this, so the router never sees it; assert that.
- Staff domain. Hand off to the staff rule (Staff rule: a dupezero.com Google Workspace account becomes super admin #1401).
- Invite token in the start cookie. Validate it with the existing invite-accept checks: token hash, not expired, not accepted. Then go to
/joinfor that invite.- If the token is valid but its email differs from the Google email, show the existing "this invite is for another email" copy.
- Pending invites matching the verified email. Look for invites whose stored email, lowercased, exactly equals the Google email lowercased, with
accepted_atnull andexpires_atin the future.- No Gmail dot or plus folding.
- Requires
email_verified. - One or more matches go to
/join. - If only expired matches exist, show the expired-invite page.
- A password user with the same email. Refuse, and show the linking copy. Never auto-link.
- Otherwise: founder. If
ERWEB_SELF_SERVE_SIGNUP_ENABLEDis on, go to/welcome; if it's off, show "Ask your admin".
-
Hold the claims while the user decides. The router does not create the user row until the person commits: they click Join, or they finish
/welcome.- Until then, the verified claims sit in a short-lived (15-minute), signed, httpOnly pending-identity cookie.
/joinand/welcomeread it. - That way, abandoning the flow leaves no orphan user.
- Until then, the verified claims sit in a short-lived (15-minute), signed, httpOnly pending-identity cookie.
-
What Join does.
POST /api/auth/jointakes the invite ids and the pending-identity cookie. For each invite, inside one transaction:- Re-check that the invite is still pending.
- Create the user if needed, with no password.
- Create the identity.
- Insert
org_memberswith the invite's role. - Set
accepted_at. - Write
user_onboarding(kind=member)for First-run state and the tour offer, per user per workspace #1404.
Then call
createSession, clear the pending cookie, and land the user in the first joined org.- Reuse the accept logic behind
api/invites/*rather than copying it. - The operation is idempotent: a double submit must not create a second membership.
-
A returning user who opens an invite link to another workspace, with a session already, also sees
/joinfor that one workspace. Join only adds the membership. -
Audit
auth.first_signinwithpath, one of:staff,invite_link,invite_email,invite_expired,password_conflict,founderorno_access. Use the org where there is one, and never the email. -
Admin copy. In Settings → Members, after an invite is created (
api/orgs/[org]/members/route.tsreturns{link, emailed}), add the line "They can also sign in with Google at dupezero.com using this email — no link needed." -
The
/joinpage. This ticket builds it functionally. Cinematic login, /welcome (founder) and /join (member) screens #1403 makes it cinematic.
Acceptance criteria
Every case below is a Playwright test using the mock IdP:
- An invite link and a matching Google email:
/join, then Join, which leaves the member at the invite's role. - No link, but a pending invite for that email:
/join. This is the main member path. - Two pending invites, for different orgs: both are listed, and joining both gives two memberships.
- Only an expired invite: the expired page, with "create my own" as a secondary option.
- A password account with the same email: the refusal copy, and no session.
- No match with the signup flag on:
/welcome. With the flag off: "Ask your admin". - Asserted directly: a user with a pending matching invite can never reach
/welcome, including by typing the URL. - An invite for
[email protected]can't be claimed by a Google account withemail_verified=false, or with any other email. - Abandoning
/joincreates no user row. Clicking Join twice creates one membership. - Axe
SCREENSincludes/joinand the expired-invite page.
Out of scope
- The cinematic styling (Cinematic login, /welcome (founder) and /join (member) screens #1403).
- Company domain claim and "request to join". These were deliberately not chosen; a future ticket can add them if duplicate tenants show up.
Depends on
#1399. This ticket blocks #1401, #1402, #1403 and #1407.
Where to start
frontend/src/lib/db/schema.ts:invitesat L92-109.frontend/src/app/(auth)/invite/[token]/page.tsx,frontend/src/app/api/invites/.frontend/src/app/api/orgs/[org]/members/route.tsL43-66.frontend/src/app/app/page.tsx, for the current landing fallbacks.
Estimate: 1.0 engineer-weeks.
Part of the E31 epic (#1396) (linked as parent). Decisions recorded 2026-11-28 with the owner.
Activity