Skip to content

First-sign-in router: join an existing workspace vs found a new one #1400

Backlog

Description

@athvin

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

  1. 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.

  2. It runs these checks in this order and stops at the first match:

    1. Identity found. The callback already handled this, so the router never sees it; assert that.
    2. Staff domain. Hand off to the staff rule (Staff rule: a dupezero.com Google Workspace account becomes super admin #1401).
    3. Invite token in the start cookie. Validate it with the existing invite-accept checks: token hash, not expired, not accepted. Then go to /join for 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.
    4. Pending invites matching the verified email. Look for invites whose stored email, lowercased, exactly equals the Google email lowercased, with accepted_at null and expires_at in 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.
    5. A password user with the same email. Refuse, and show the linking copy. Never auto-link.
    6. Otherwise: founder. If ERWEB_SELF_SERVE_SIGNUP_ENABLED is on, go to /welcome; if it's off, show "Ask your admin".
  3. 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. /join and /welcome read it.
    • That way, abandoning the flow leaves no orphan user.
  4. What Join does. POST /api/auth/join takes the invite ids and the pending-identity cookie. For each invite, inside one transaction:

    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.
  5. A returning user who opens an invite link to another workspace, with a session already, also sees /join for that one workspace. Join only adds the membership.

  6. Audit auth.first_signin with path, one of: staff, invite_link, invite_email, invite_expired, password_conflict, founder or no_access. Use the org where there is one, and never the email.

  7. Admin copy. In Settings → Members, after an invite is created (api/orgs/[org]/members/route.ts returns {link, emailed}), add the line "They can also sign in with Google at dupezero.com using this email — no link needed."

  8. The /join page. 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 with email_verified=false, or with any other email.
  • Abandoning /join creates no user row. Clicking Join twice creates one membership.
  • Axe SCREENS includes /join and the expired-invite page.

Out of scope

Depends on

#1399. This ticket blocks #1401, #1402, #1403 and #1407.

Where to start

  • frontend/src/lib/db/schema.ts: invites at L92-109.
  • frontend/src/app/(auth)/invite/[token]/page.tsx, frontend/src/app/api/invites/.
  • frontend/src/app/api/orgs/[org]/members/route.ts L43-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

  1. added
    post-mvp
    on Nov 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    post-mvp

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions