|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState } from "react"; |
| 4 | +import { useRouter } from "next/navigation"; |
| 5 | +import Link from "next/link"; |
| 6 | +import { createClient } from "@/lib/supabase/client"; |
| 7 | +import { Button } from "@/components/ui/button"; |
| 8 | + |
| 9 | +interface InviteAcceptProps { |
| 10 | + inviteId: string; |
| 11 | + workspaceSlug: string; |
| 12 | + email: string; |
| 13 | + isAuthenticated: boolean; |
| 14 | + userEmail: string | null; |
| 15 | +} |
| 16 | + |
| 17 | +export function InviteAccept({ |
| 18 | + inviteId, |
| 19 | + workspaceSlug, |
| 20 | + email, |
| 21 | + isAuthenticated, |
| 22 | + userEmail, |
| 23 | +}: InviteAcceptProps) { |
| 24 | + const router = useRouter(); |
| 25 | + const [accepting, setAccepting] = useState(false); |
| 26 | + const [error, setError] = useState<string | null>(null); |
| 27 | + |
| 28 | + // Not authenticated — prompt to sign in or sign up |
| 29 | + if (!isAuthenticated) { |
| 30 | + return ( |
| 31 | + <div className="flex flex-col gap-3"> |
| 32 | + <p className="text-sm text-muted-foreground"> |
| 33 | + Sign in or create an account with{" "} |
| 34 | + <span className="font-medium text-foreground">{email}</span> to |
| 35 | + accept this invite. |
| 36 | + </p> |
| 37 | + <div className="flex gap-2"> |
| 38 | + <Button render={<Link href="/sign-in" />}>Sign in</Button> |
| 39 | + <Button variant="outline" render={<Link href="/sign-up" />}> |
| 40 | + Sign up |
| 41 | + </Button> |
| 42 | + </div> |
| 43 | + </div> |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + // Authenticated but with a different email |
| 48 | + const emailMismatch = |
| 49 | + userEmail && userEmail.toLowerCase() !== email.toLowerCase(); |
| 50 | + |
| 51 | + if (emailMismatch) { |
| 52 | + return ( |
| 53 | + <div className="flex flex-col gap-3"> |
| 54 | + <p className="text-sm text-muted-foreground"> |
| 55 | + This invite was sent to{" "} |
| 56 | + <span className="font-medium text-foreground">{email}</span>, but |
| 57 | + you're signed in as{" "} |
| 58 | + <span className="font-medium text-foreground">{userEmail}</span>. |
| 59 | + </p> |
| 60 | + <p className="text-sm text-muted-foreground"> |
| 61 | + Sign in with the invited email to accept. |
| 62 | + </p> |
| 63 | + </div> |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + async function handleAccept() { |
| 68 | + setAccepting(true); |
| 69 | + setError(null); |
| 70 | + |
| 71 | + const supabase = createClient(); |
| 72 | + |
| 73 | + // Use the security definer RPC which atomically marks the invite as |
| 74 | + // accepted and inserts the member with the correct role from the invite. |
| 75 | + const { error: acceptError } = await supabase.rpc("accept_invite", { |
| 76 | + invite_id: inviteId, |
| 77 | + }); |
| 78 | + |
| 79 | + if (acceptError) { |
| 80 | + // Duplicate key means already a member — treat as success |
| 81 | + if (acceptError.message.includes("duplicate key")) { |
| 82 | + router.push(`/${workspaceSlug}`); |
| 83 | + return; |
| 84 | + } |
| 85 | + setError(acceptError.message); |
| 86 | + setAccepting(false); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + router.push(`/${workspaceSlug}`); |
| 91 | + } |
| 92 | + |
| 93 | + return ( |
| 94 | + <div className="flex flex-col gap-3"> |
| 95 | + <p className="text-sm text-muted-foreground"> |
| 96 | + You're signed in as{" "} |
| 97 | + <span className="font-medium text-foreground">{userEmail}</span>. |
| 98 | + </p> |
| 99 | + {error && <p className="text-xs text-destructive">{error}</p>} |
| 100 | + <Button onClick={handleAccept} disabled={accepting}> |
| 101 | + {accepting ? "Joining…" : "Accept invite"} |
| 102 | + </Button> |
| 103 | + </div> |
| 104 | + ); |
| 105 | +} |
0 commit comments