Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
fix(team): return the direct invite link so shared invites don't 404 (#340)
Gökhan Geyik8f7a880cb9b8
diff --git a/web/src/components/settings/team-section.tsx b/web/src/components/settings/team-section.tsxindex 03b119dd6..8e10d055b 100644--- a/web/src/components/settings/team-section.tsx+++ b/web/src/components/settings/team-section.tsx@@ -417,11 +417,13 @@ function InviteDialog({ canInvite, onInvited }: { canInvite: boolean; onInvited:const [role, setRole] = useState<TeamRole>('analyst');const [submitting, setSubmitting] = useState(false);const [inviteLink, setInviteLink] = useState<string | null>(null);+ const [emailSent, setEmailSent] = useState(true);function reset() {setEmail('');setRole('analyst');setInviteLink(null);+ setEmailSent(true);}async function handleSubmit() {@@ -431,9 +433,10 @@ function InviteDialog({ canInvite, onInvited }: { canInvite: boolean; onInvited:}setSubmitting(true);try {- const { inviteLink: link } = await inviteMember(email.trim(), role);+ const { inviteLink: link, emailSent: sent } = await inviteMember(email.trim(), role);setInviteLink(link);- toast.success('Invitation sent');+ setEmailSent(sent);+ toast.success(sent ? 'Invitation sent' : 'Invite created — share the link');onInvited();} catch (error) {toast.error(error instanceof Error ? error.message : 'Failed to send invitation');@@ -475,7 +478,9 @@ function InviteDialog({ canInvite, onInvited }: { canInvite: boolean; onInvited:{inviteLink ? (<div className="space-y-3"><p className="text-sm text-muted-foreground">- Invitation sent. You can also share this link directly:+ {emailSent+ ? 'Invitation sent. You can also share this link directly:'+ : 'No email was sent — this address may already have an account. Share this link with them directly:'}</p><div className="flex items-center gap-2"><Input readOnly value={inviteLink} className="flex-1 text-xs" />diff --git a/web/src/lib/actions/team.ts b/web/src/lib/actions/team.tsindex bc3f8f228..33c28936c 100644--- a/web/src/lib/actions/team.ts+++ b/web/src/lib/actions/team.ts@@ -202,14 +202,25 @@ export async function inviteMember(email: string, role: TeamRole) {const token = randomBytes(32).toString('hex');const appUrl = process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000';- // What we hand to Supabase as `redirectTo`. It becomes `{{ .RedirectTo }}`- // in the Invite User template, which we use as the *base* for the- // /auth/confirm URL — token_hash + type get appended in the template.- // The benefit: the prefix is whatever appUrl is for *this* environment- // (localhost when running `pnpm dev`, app.ansvisor.com in production),- // independent of Supabase's project-level Site URL. So a single template- // works for both dev and prod.- const inviteLink = `${appUrl}/auth/confirm?next=${encodeURIComponent(`/invite/${token}`)}`;++ // The link we surface to the admin (dashboard "share this link directly" ++ // clipboard) is the direct, self-contained accept page. It looks the+ // invitation up by token and routes to sign-up/accept on its own, so it+ // NEVER depends on Supabase's email OTP params.+ //+ // Previously this returned the /auth/confirm base below, which only works+ // once the invite EMAIL template appends token_hash + type. Opened directly+ // (which is exactly what the "share this link" affordance does) it has+ // neither, so /auth/confirm bounced to /sign-in?error=auth_confirm_missing_params.+ // resendInvitation already returned this direct form; inviteMember now matches.+ const shareLink = `${appUrl}/invite/${token}`;++ // What we hand to Supabase as `redirectTo` for the invite EMAIL only. It+ // becomes `{{ .RedirectTo }}` in the Invite User template, which appends+ // token_hash + type; /auth/confirm then verifies the OTP server-side and+ // forwards to /invite/{token}. The prefix is this environment's appUrl, so+ // one template works for both dev and prod.+ const emailRedirectTo = `${appUrl}/auth/confirm?next=${encodeURIComponent(`/invite/${token}`)}`;const { data: invitation, error: insertError } = await supabaseAdmin.from('invitations')@@ -230,18 +241,30 @@ export async function inviteMember(email: string, role: TeamRole) {throw new Error(insertError.message);}+ // 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. Surface whether it actually sent+ // so the UI can be honest ("share this link" vs. "email sent") instead of+ // always claiming an email went out.+ let emailSent = false;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.+ emailSent = true;+ } catch (err) {+ // Existing-user or SMTP failure — the invitation row is saved and the admin+ // shares `shareLink` directly. Log the invitation/org id (not the invitee's+ // email — that's PII in persistent logs) plus the error for diagnostics.+ console.warn(+ `[invite] Supabase invite email not sent (invitation=${invitation.id}, org=${profile.organization_id}) — existing user or SMTP`,+ err,+ );}revalidatePath('/dashboard/settings');- return { invitation, inviteLink };+ return { invitation, inviteLink: shareLink, emailSent };}export async function revokeInvitation(invitationId: string) {