Repository navigation
fix(invite): accept-link session bug + force password setup on join - #127
Merged
Merged
Conversation
…ive, prompt for password
Symptom: clicking an invite mail bounced the user to /sign-up, then to
/sign-in with credentials that didn't work. Two stacked bugs:
1. The invite mail's redirect_to pointed straight at /invite/<token>.
Supabase appends ?code=<session-code> to that URL after verifying the
magic link, but our /invite page is a server component that calls
supabase.auth.getUser() with no code-exchange step in between. It saw
no session, fell into its 'if (!user)' branch, and ejected the user
to /sign-up. The /sign-up form then called signUp() on an email that
already existed (the invite created the auth row), got a soft success
without actually setting credentials, and pushed the user to /sign-in
where their typed password didn't match anything.
2. inviteUserByEmail creates an auth.users row with no password. Even
when the session is good, the user has nothing to sign back in with
later. The accept card just had a 'Join' button — no password setup.
Fix:
- team.ts: invite mail's redirect_to is now /auth/callback?next=/invite/<token>.
/auth/callback already calls exchangeCodeForSession and forwards to
next, so by the time the user lands on the invite page they have a
cookie session and the !user branch never fires.
- AcceptInvitationCard: turned the single button into a small form —
full name (optional), password + confirm (required, min 8). On submit
it updateUser({ password, full_name }) before calling acceptInvitation,
so the moment the invite flips to 'accepted' the credentials are good.
gkhngyk
added a commit
that referenced
this pull request
May 30, 2026
…ash-flow, not PKCE (#129) * fix(invite): use verifyOtp via /auth/confirm — invite mails ship hash-flow tokens, not PKCE codes The /auth/callback hop added in #127 only works for PKCE OAuth flows where the browser holds the code verifier. Admin-initiated invite emails (supabase.auth.admin.inviteUserByEmail) have no PKCE verifier on the recipient's browser — Supabase's verify endpoint returns the session as URL hash params (#access_token=...&refresh_token=...) which server components can't read. /auth/callback sees no ?code=, redirects to /sign-in?error=auth_callback_failed, and the user falls into the old broken /sign-up dance again. Canonical fix is the OTP-token-hash flow Supabase documents for SSR: - New /auth/confirm route handler reads ?token_hash + ?type + ?redirect_to, calls supabase.auth.verifyOtp({ type, token_hash }), and 302s to the destination once the session cookie is set server-side. Works for every Supabase email type (invite, signup, recovery, magiclink, email_change) — the type comes from the template, not from us. - team.ts inviteLink is now plain ${appUrl}/invite/<token> again. The auth hop is no longer in the redirect; it's in the email template's link itself. Required ops step (manual, not in code): the Supabase project's Auth → Email Templates → 'Invite user' template needs the link changed from {{ .ConfirmationURL }} to: {{ .SiteURL }}/auth/confirm?token_hash={{ .TokenHash }}&type=invite&redirect_to={{ .RedirectTo }} Same swap recommended for signup / recovery / magiclink templates so every email auth flow goes through the same SSR-friendly path. Until that's done the invite link still hits the old default URL and the fix doesn't take effect. * fix(invite): use {{ .RedirectTo }}-based template prefix so a single template covers dev + prod Previous attempt put /auth/confirm under {{ .SiteURL }} in the email template. Supabase's Site URL is one project-wide value (production https://app.ansvisor.com here), so the same template can't drive both prod testing and a localhost dev loop — the link always points at prod regardless of NEXT_PUBLIC_APP_URL. Switch the redirectTo we pass to inviteUserByEmail to be the full /auth/confirm URL with our next path baked in: ${appUrl}/auth/confirm?next=/invite/<token> Template now uses {{ .RedirectTo }} as the prefix and only appends the OTP params: {{ .RedirectTo }}&token_hash={{ .TokenHash }}&type=invite Because appUrl resolves to whichever NEXT_PUBLIC_APP_URL is in the current environment, the link goes to localhost in dev and prod in prod — without changing Supabase project settings between the two.
gkhngyk
added a commit
that referenced
this pull request
May 30, 2026
…ntl rewrite was 404ing (#130) * fix(invite): use verifyOtp via /auth/confirm — invite mails ship hash-flow tokens, not PKCE codes The /auth/callback hop added in #127 only works for PKCE OAuth flows where the browser holds the code verifier. Admin-initiated invite emails (supabase.auth.admin.inviteUserByEmail) have no PKCE verifier on the recipient's browser — Supabase's verify endpoint returns the session as URL hash params (#access_token=...&refresh_token=...) which server components can't read. /auth/callback sees no ?code=, redirects to /sign-in?error=auth_callback_failed, and the user falls into the old broken /sign-up dance again. Canonical fix is the OTP-token-hash flow Supabase documents for SSR: - New /auth/confirm route handler reads ?token_hash + ?type + ?redirect_to, calls supabase.auth.verifyOtp({ type, token_hash }), and 302s to the destination once the session cookie is set server-side. Works for every Supabase email type (invite, signup, recovery, magiclink, email_change) — the type comes from the template, not from us. - team.ts inviteLink is now plain ${appUrl}/invite/<token> again. The auth hop is no longer in the redirect; it's in the email template's link itself. Required ops step (manual, not in code): the Supabase project's Auth → Email Templates → 'Invite user' template needs the link changed from {{ .ConfirmationURL }} to: {{ .SiteURL }}/auth/confirm?token_hash={{ .TokenHash }}&type=invite&redirect_to={{ .RedirectTo }} Same swap recommended for signup / recovery / magiclink templates so every email auth flow goes through the same SSR-friendly path. Until that's done the invite link still hits the old default URL and the fix doesn't take effect. * fix(invite): use {{ .RedirectTo }}-based template prefix so a single template covers dev + prod Previous attempt put /auth/confirm under {{ .SiteURL }} in the email template. Supabase's Site URL is one project-wide value (production https://app.ansvisor.com here), so the same template can't drive both prod testing and a localhost dev loop — the link always points at prod regardless of NEXT_PUBLIC_APP_URL. Switch the redirectTo we pass to inviteUserByEmail to be the full /auth/confirm URL with our next path baked in: ${appUrl}/auth/confirm?next=/invite/<token> Template now uses {{ .RedirectTo }} as the prefix and only appends the OTP params: {{ .RedirectTo }}&token_hash={{ .TokenHash }}&type=invite Because appUrl resolves to whichever NEXT_PUBLIC_APP_URL is in the current environment, the link goes to localhost in dev and prod in prod — without changing Supabase project settings between the two. * fix(invite): mirror /auth/confirm under [locale]/(marketing) so next-intl can resolve it PR #129 added /auth/confirm at the root app directory only. next-intl middleware rewrites every /auth/* request to /[locale]/auth/*, and the deployed bundle had no matching file under [locale]/auth/confirm — so the request landed on the framework 404 page. The existing /auth/callback works because there's already a [locale]/(marketing)/auth/callback/route.ts that the rewrite resolves to. Add the equivalent mirror for confirm so the same routing rules have a destination. Verified via response headers — /auth/callback shows `x-matched-path: /[locale]/auth/callback`, /auth/confirm before this change matched /404 with x-vercel-cache HIT. Keeping the root /auth/confirm/route.ts around to stay consistent with the callback sibling pattern; it's never hit but matches the existing convention.
Closed
7 tasks
This branch was successfully deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Clicking an invite mail used to bounce the user to
/sign-up, then to/sign-inwith credentials that didn't work. Two stacked bugs.Bug A — session never established on
/invite/<token>The invite mail's
redirect_topointed straight at our/invite/<token>page. After Supabase verified the magic-link token, it appended?code=<session-code>to that URL and redirected the user there. But our/invite/[token]/page.tsxis a server component —supabase.auth.getUser()returns null because the?code=hadn't been exchanged for a cookie session yet. Theif (!user)branch fired and ejected the user to/sign-up?invite=....On
/sign-up, the form calledsupabase.auth.signUp({ email, password })on an email that already existed (created byinviteUserByEmail). Supabase returned a soft success without actually changing credentials, and the form pushed the user to/sign-in?verified=pending. The password they typed didn't match anything in the DB.Bug B — invited users have no password to sign back in with
inviteUserByEmailcreates anauth.usersrow with noencrypted_password. Even if Bug A hadn't existed, the user would land on the accept card with a single 'Join' button, accept, get into the dashboard once via the magic-link session — then never be able to sign back in. They have nothing to type at the sign-in form.Why
This is the canonical broken-invite-flow that every customer's first invited teammate would have hit. The team-invite UI itself works (the mail goes out via Resend) so it looks healthy from the inviter's side — the breakage is entirely downstream, on the invitee's first click.
Scope
team.ts: the invite link ininviteMemberis now/auth/callbackalready callsexchangeCodeForSessionand forwards tonext. So by the time the user lands on/invite/<token>they have a cookie session and the!userbranch never fires.AcceptInvitationCard: the single 'Join' button is now a small form — Full name (optional), Password + Confirm Password (required, min 8 chars). On submit it doesOrder matters: if
updateUserfails we never flip the invitation to 'accepted' — the user can retry the link instead of landing in a half-joined state.Test plan
auth.users/invitationsfor the test email/invite/<token>directly (no detour through/sign-up)/dashboardas a member of the inviter's orgOut of scope
/sign-up?invite=...fallback path in/invite/[token]/page.tsx(line ~32). With the redirect fix this path shouldn't fire for normal invite clicks; if someone lands on/invite/<token>directly without a session it's still the same old broken redirect. Out of scope here, easy follow-up if anyone hits it.auth.users— separate concern, separate fix.