Skip to content

fix(team): return the direct invite link so shared invites don't 404 - #340

Merged
gkhngyk merged 2 commits into
mainfrom
fix/invite-share-link
Jul 4, 2026
Merged

gkhngyk merged 2 commits into
mainfrom
fix/invite-share-link

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes a broken team-invite link. When an admin invited a member and used the dashboard's "share this link directly" affordance (copy button), the copied URL was …/auth/confirm?next=/invite/{token} — which redirected the invitee to …/sign-in?error=auth_confirm_missing_params.

Root cause: /auth/confirm only works when Supabase's invite email template appends token_hash + type. inviteMember was handing that same /auth/confirm base back to the dashboard as the shareable link, so opening it directly (no token_hash/type) hit the "missing params" guard. resendInvitation already returned the correct direct form; inviteMember now matches.

Changes (web/src/lib/actions/team.ts)

  • Return the direct /invite/{token} accept link for the admin to share. That page looks the invitation up by token and routes to sign-up/accept on its own — no Supabase OTP params — so it works for new users, signed-out users, and existing users being re-invited alike.
  • Keep /auth/confirm?next=… as the email redirectTo only (renamed emailRedirectTo for clarity), where the invite email template decorates it with token_hash + type.
  • Replace the silent catch {} around inviteUserByEmail with a console.warn. inviteUserByEmail only emails brand-new addresses and errors for already-registered emails (a Supabase limitation) — expected, since the share link is the delivery path, but genuine SMTP failures for new users are now diagnosable instead of vanishing.

Re-inviting a removed member

This also unblocks the "removed then re-invited" case raised in review:

  • removeMember detaches the profile (organization_id = null) but keeps the auth user + profile row.
  • The partial unique index invitations_org_email_pending_idx only blocks duplicate pending invites, so a fresh invite for a previously-accepted/removed user inserts fine.
  • acceptInvitation re-attaches the existing profile to the org, so opening the direct link and signing in re-joins them.

Note: auto-emailing existing users (vs. sharing the link) is intentionally out of scope — the app has no transactional email provider beyond Supabase Auth, and Supabase can't send an invite email to an already-registered address. Tracked as a possible follow-up (magic-link or Resend).

Type of change

  • fix — Bug fix

How to test

  1. As an admin, invite a member (a brand-new email, and separately an email that already has an account).
  2. In the post-invite dialog, copy the "share this link directly" link → it is now …/invite/{token} (not /auth/confirm?…).
  3. Open it: signed-out → sign-up/sign-in then the accept card; signed-in with the invited email → accept card. No auth_confirm_missing_params.
  4. Remove a member, then re-invite the same email → the new invite is created and the link accepts them back into the org.
  5. yarn typecheck, yarn lint, yarn format:check pass from web/.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (from web/)
  • yarn typecheck passes (from web/)
  • yarn format:check passes (from web/)
  • Changes are focused — one concern per PR

inviteMember returned the dashboard "share this link directly" URL as
`/auth/confirm?next=/invite/{token}`. That base only works once the
Supabase invite EMAIL template appends token_hash + type; opened directly
(exactly what the copy/share affordance does) it has neither, so
/auth/confirm bounced to /sign-in?error=auth_confirm_missing_params.

- Return the direct, self-contained `/invite/{token}` accept link for the
  admin to share (matches what resendInvitation already returned). That
  page looks the invitation up by token and routes to sign-up/accept on
  its own — no Supabase OTP params needed — so it works for new users,
  signed-out users, and existing users being re-invited alike.
- Keep `/auth/confirm?next=...` as the EMAIL `redirectTo` only (renamed to
  `emailRedirectTo` for clarity), where the template decorates it.
- Replace the silent `catch {}` around inviteUserByEmail with a warn log:
  it only emails brand-new addresses and errors for already-registered
  users (Supabase limitation) — that's expected, the share link is the
  path, but genuine SMTP failures for new users are now diagnosable.

Re-inviting a removed member (profile detached via organization_id=null)
now works end to end: the partial unique index only blocks *pending*
dupes, and acceptInvitation re-attaches the existing profile to the org.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes the team invite “share this link directly” URL by returning a direct /invite/{token} link from inviteMember, avoiding reliance on Supabase email-only OTP parameters that caused /auth/confirm links to 404 when opened directly.

Changes:

  • Return a direct, self-contained /invite/{token} link for admins to copy/share from the dashboard.
  • Keep /auth/confirm?next=... as the Supabase redirectTo value intended for email-template augmentation (token_hash/type).
  • Replace the silent inviteUserByEmail error swallow with a warning log for diagnosability.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +256 to +260
console.warn(
`[invite] Supabase invite email not sent for ${normalizedEmail} (existing user or SMTP): ${
err instanceof Error ? err.message : String(err)
}`,
);
Comment on lines 244 to 265
try {
await supabaseAdmin.auth.admin.inviteUserByEmail(normalizedEmail, {
redirectTo: inviteLink,
redirectTo: emailRedirectTo,
data: { invitation_token: token },
});
} catch {
// Supabase may fail if user already has an account; we still keep the
// invitation row so that the direct link works.
} catch (err) {
// inviteUserByEmail only emails BRAND-NEW addresses — it errors for an
// email that's already registered (Supabase can't "create" that user), so
// existing users never get the Supabase invite mail. That's acceptable:
// the invitation row is saved and the admin shares `shareLink` directly.
// Log the reason so genuine SMTP failures for new users stay diagnosable
// instead of vanishing into a silent catch.
console.warn(
`[invite] Supabase invite email not sent for ${normalizedEmail} (existing user or SMTP): ${
err instanceof Error ? err.message : String(err)
}`,
);
}

revalidatePath('/dashboard/settings');
return { invitation, inviteLink };
return { invitation, inviteLink: shareLink };
}
- inviteMember now returns `emailSent` (true only when inviteUserByEmail
  didn't throw). The invite dialog uses it: shows "Invitation sent … share
  this link" when an email went out, and "No email was sent — this address
  may already have an account. Share this link with them directly" when it
  didn't, instead of always claiming an email was sent.
- The swallowed-error log no longer includes the invitee's email address
  (PII in persistent logs) — it logs invitation id + org id and passes the
  error object for stack/diagnostics.

Addresses the Copilot review on #340.
@gkhngyk
gkhngyk merged commit 8f7a880 into main Jul 4, 2026
4 checks passed
@gkhngyk
gkhngyk deleted the fix/invite-share-link branch July 4, 2026 16:39
@gkhngyk gkhngyk mentioned this pull request Jul 6, 2026
5 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants