|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { Link2, Loader2, Unlink } from "lucide-react"; |
| 4 | +import { useCallback, useEffect, useState } from "react"; |
| 5 | +import { toast } from "sonner"; |
| 6 | +import { Button } from "@/components/ui/button"; |
| 7 | +import { |
| 8 | + Card, |
| 9 | + CardContent, |
| 10 | + CardDescription, |
| 11 | + CardHeader, |
| 12 | + CardTitle, |
| 13 | +} from "@/components/ui/card"; |
| 14 | +import { authClient } from "@/lib/auth-client"; |
| 15 | + |
| 16 | +const LINKING_CALLBACK_URL = "/dashboard/settings/profile"; |
| 17 | + |
| 18 | +const TRUSTED_PROVIDERS = ["google", "github"] as const; |
| 19 | +type SocialProvider = (typeof TRUSTED_PROVIDERS)[number]; |
| 20 | + |
| 21 | +type AccountItem = { |
| 22 | + providerId: string; |
| 23 | + accountId?: string; |
| 24 | +}; |
| 25 | + |
| 26 | +function providerLabel(providerId: string): string { |
| 27 | + return providerId.charAt(0).toUpperCase() + providerId.slice(1); |
| 28 | +} |
| 29 | + |
| 30 | +export function LinkingAccount() { |
| 31 | + const [accounts, setAccounts] = useState<AccountItem[]>([]); |
| 32 | + const [accountsLoading, setAccountsLoading] = useState(true); |
| 33 | + const [linkingProvider, setLinkingProvider] = useState<SocialProvider | null>( |
| 34 | + null, |
| 35 | + ); |
| 36 | + const [unlinkingProviderId, setUnlinkingProviderId] = useState<string | null>( |
| 37 | + null, |
| 38 | + ); |
| 39 | + |
| 40 | + const fetchAccounts = useCallback(async () => { |
| 41 | + setAccountsLoading(true); |
| 42 | + try { |
| 43 | + const { data } = await authClient.listAccounts(); |
| 44 | + const list = Array.isArray(data) |
| 45 | + ? data |
| 46 | + : ((data && typeof data === "object" && "accounts" in data |
| 47 | + ? (data as { accounts?: AccountItem[] }).accounts |
| 48 | + : null) ?? []); |
| 49 | + setAccounts(Array.isArray(list) ? list : []); |
| 50 | + } catch { |
| 51 | + setAccounts([]); |
| 52 | + } finally { |
| 53 | + setAccountsLoading(false); |
| 54 | + } |
| 55 | + }, []); |
| 56 | + |
| 57 | + useEffect(() => { |
| 58 | + fetchAccounts(); |
| 59 | + }, [fetchAccounts]); |
| 60 | + |
| 61 | + const linkedProviderIds = new Set(accounts.map((a) => a.providerId)); |
| 62 | + const socialAccounts = accounts.filter((a) => |
| 63 | + TRUSTED_PROVIDERS.includes(a.providerId as SocialProvider), |
| 64 | + ); |
| 65 | + |
| 66 | + const handleLinkSocial = async (provider: SocialProvider) => { |
| 67 | + setLinkingProvider(provider); |
| 68 | + try { |
| 69 | + const { error } = await authClient.linkSocial({ |
| 70 | + provider, |
| 71 | + callbackURL: LINKING_CALLBACK_URL, |
| 72 | + }); |
| 73 | + if (error) { |
| 74 | + toast.error(error.message ?? "Failed to link account"); |
| 75 | + setLinkingProvider(null); |
| 76 | + return; |
| 77 | + } |
| 78 | + } catch (err) { |
| 79 | + toast.error( |
| 80 | + "Failed to link account", |
| 81 | + err instanceof Error ? { description: err.message } : undefined, |
| 82 | + ); |
| 83 | + setLinkingProvider(null); |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + const handleUnlink = async (providerId: string, accountId?: string) => { |
| 88 | + setUnlinkingProviderId(providerId); |
| 89 | + try { |
| 90 | + const { error } = await authClient.unlinkAccount({ |
| 91 | + providerId, |
| 92 | + ...(accountId && { accountId }), |
| 93 | + }); |
| 94 | + if (error) { |
| 95 | + toast.error(error.message ?? "Failed to unlink account"); |
| 96 | + return; |
| 97 | + } |
| 98 | + toast.success("Account unlinked"); |
| 99 | + await fetchAccounts(); |
| 100 | + } catch (err) { |
| 101 | + toast.error( |
| 102 | + "Failed to unlink account", |
| 103 | + err instanceof Error ? { description: err.message } : undefined, |
| 104 | + ); |
| 105 | + } finally { |
| 106 | + setUnlinkingProviderId(null); |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + const canUnlink = accounts.length > 1; |
| 111 | + |
| 112 | + return ( |
| 113 | + <Card className="h-full bg-sidebar p-2.5 rounded-xl max-w-6xl mx-auto w-full"> |
| 114 | + <div className="rounded-xl bg-background shadow-md"> |
| 115 | + <CardHeader> |
| 116 | + <div className="flex flex-row gap-2 flex-wrap justify-between items-center"> |
| 117 | + <div> |
| 118 | + <CardTitle className="text-xl flex flex-row gap-2"> |
| 119 | + <Link2 className="size-6 text-muted-foreground self-center" /> |
| 120 | + Linking account |
| 121 | + </CardTitle> |
| 122 | + <CardDescription> |
| 123 | + Link your Google or GitHub account to sign in with them. |
| 124 | + </CardDescription> |
| 125 | + </div> |
| 126 | + </div> |
| 127 | + </CardHeader> |
| 128 | + <CardContent className="space-y-6 py-8 border-t"> |
| 129 | + {/* Linked accounts */} |
| 130 | + <div className="space-y-2"> |
| 131 | + <p className="text-sm font-medium">Linked accounts</p> |
| 132 | + {accountsLoading ? ( |
| 133 | + <div className="flex items-center gap-2 text-sm text-muted-foreground py-2"> |
| 134 | + <Loader2 className="size-4 animate-spin" /> |
| 135 | + Loading... |
| 136 | + </div> |
| 137 | + ) : socialAccounts.length === 0 ? ( |
| 138 | + <p className="text-sm text-muted-foreground py-2"> |
| 139 | + No social accounts linked yet. |
| 140 | + </p> |
| 141 | + ) : ( |
| 142 | + <ul className="space-y-2"> |
| 143 | + {socialAccounts.map((acc) => ( |
| 144 | + <li |
| 145 | + key={acc.accountId ?? acc.providerId} |
| 146 | + className="flex items-center justify-between rounded-lg border px-3 py-2 text-sm" |
| 147 | + > |
| 148 | + <span className="font-medium"> |
| 149 | + {providerLabel(acc.providerId)} |
| 150 | + </span> |
| 151 | + {canUnlink && ( |
| 152 | + <Button |
| 153 | + variant="ghost" |
| 154 | + size="sm" |
| 155 | + className="text-destructive hover:text-destructive hover:bg-destructive/10" |
| 156 | + onClick={() => |
| 157 | + handleUnlink(acc.providerId, acc.accountId) |
| 158 | + } |
| 159 | + disabled={unlinkingProviderId === acc.providerId} |
| 160 | + isLoading={unlinkingProviderId === acc.providerId} |
| 161 | + > |
| 162 | + {unlinkingProviderId === acc.providerId ? ( |
| 163 | + <Loader2 className="size-4 animate-spin" /> |
| 164 | + ) : ( |
| 165 | + <> |
| 166 | + <Unlink className="mr-1.5 size-4" /> |
| 167 | + Unlink |
| 168 | + </> |
| 169 | + )} |
| 170 | + </Button> |
| 171 | + )} |
| 172 | + </li> |
| 173 | + ))} |
| 174 | + </ul> |
| 175 | + )} |
| 176 | + </div> |
| 177 | + |
| 178 | + <p className="text-sm text-muted-foreground"> |
| 179 | + Click a provider below to link it to your account. You will be |
| 180 | + redirected to complete the flow. |
| 181 | + </p> |
| 182 | + <div className="flex flex-wrap gap-3"> |
| 183 | + {!linkedProviderIds.has("google") && ( |
| 184 | + <Button |
| 185 | + variant="outline" |
| 186 | + type="button" |
| 187 | + className="min-w-[180px]" |
| 188 | + onClick={() => handleLinkSocial("google")} |
| 189 | + disabled={!!linkingProvider} |
| 190 | + isLoading={linkingProvider === "google"} |
| 191 | + > |
| 192 | + {linkingProvider === "google" ? ( |
| 193 | + <Loader2 className="mr-2 size-4 animate-spin" /> |
| 194 | + ) : ( |
| 195 | + <svg viewBox="0 0 24 24" className="mr-2 size-4"> |
| 196 | + <path |
| 197 | + fill="currentColor" |
| 198 | + d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92c-.26 1.37-1.04 2.53-2.21 3.31v2.77h3.57c2.08-1.92 3.28-4.74 3.28-8.09z" |
| 199 | + /> |
| 200 | + <path |
| 201 | + fill="currentColor" |
| 202 | + d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.57-2.77c-.98.66-2.23 1.06-3.71 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84C3.99 20.53 7.7 23 12 23z" |
| 203 | + /> |
| 204 | + <path |
| 205 | + fill="currentColor" |
| 206 | + d="M5.84 14.09c-.22-.66-.35-1.36-.35-2.09s.13-1.43.35-2.09V7.07H2.18C1.43 8.55 1 10.22 1 12s.43 3.45 1.18 4.93l2.85-2.22.81-.62z" |
| 207 | + /> |
| 208 | + <path |
| 209 | + fill="currentColor" |
| 210 | + d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1 7.7 1 3.99 3.47 2.18 7.07l3.66 2.84c.87-2.6 3.3-4.53 6.16-4.53z" |
| 211 | + /> |
| 212 | + </svg> |
| 213 | + )} |
| 214 | + Link with Google |
| 215 | + </Button> |
| 216 | + )} |
| 217 | + {!linkedProviderIds.has("github") && ( |
| 218 | + <Button |
| 219 | + variant="outline" |
| 220 | + type="button" |
| 221 | + className="min-w-[180px]" |
| 222 | + onClick={() => handleLinkSocial("github")} |
| 223 | + disabled={!!linkingProvider} |
| 224 | + isLoading={linkingProvider === "github"} |
| 225 | + > |
| 226 | + {linkingProvider === "github" ? ( |
| 227 | + <Loader2 className="mr-2 size-4 animate-spin" /> |
| 228 | + ) : ( |
| 229 | + <svg |
| 230 | + viewBox="0 0 24 24" |
| 231 | + className="mr-2 size-4" |
| 232 | + fill="currentColor" |
| 233 | + > |
| 234 | + <path d="M12 0c-6.626 0-12 5.373-12 12 0 5.302 3.438 9.8 8.207 11.387.599.111.793-.261.793-.577v-2.234c-3.338.726-4.033-1.416-4.033-1.416-.546-1.387-1.333-1.756-1.333-1.756-1.089-.745.083-.729.083-.729 1.205.084 1.839 1.237 1.839 1.237 1.07 1.834 2.807 1.304 3.492.997.107-.775.418-1.305.762-1.604-2.665-.305-5.467-1.334-5.467-5.931 0-1.311.469-2.381 1.236-3.221-.124-.303-.535-1.524.117-3.176 0 0 1.008-.322 3.301 1.23.957-.266 1.983-.399 3.003-.404 1.02.005 2.047.138 3.006.404 2.291-1.552 3.297-1.23 3.297-1.23.653 1.653.242 2.874.118 3.176.77.84 1.235 1.911 1.235 3.221 0 4.609-2.807 5.624-5.479 5.921.43.372.823 1.102.823 2.222v3.293c0 .319.192.694.801.576 4.765-1.589 8.199-6.086 8.199-11.386 0-6.627-5.373-12-12-12z" /> |
| 235 | + </svg> |
| 236 | + )} |
| 237 | + Link with GitHub |
| 238 | + </Button> |
| 239 | + )} |
| 240 | + </div> |
| 241 | + </CardContent> |
| 242 | + </div> |
| 243 | + </Card> |
| 244 | + ); |
| 245 | +} |
0 commit comments