| 24 | }; |
| 25 | |
| 26 | export function LoginMethodsCard({ primaryEmail, providers, onError }: LoginMethodsCardProps) { |
| 27 | const trpc = useTRPC(); |
| 28 | const alreadyLinkedProviders = new Set(providers.map(o => o.provider)); |
| 29 | const availableProviders = LinkableAuthProviders.filter(p => !alreadyLinkedProviders.has(p.id)); |
| 30 | |
| 31 | const linkMutation = useMutation( |
| 32 | trpc.user.linkAuthProvider.mutationOptions({ |
| 33 | onSuccess: (data, variables) => { |
| 34 | void signIn(variables.provider, { |
| 35 | callbackUrl: `/connected-accounts?linked=${variables.provider}`, |
| 36 | }); |
| 37 | }, |
| 38 | onError: () => onError('LINKING-FAILED'), |
| 39 | }) |
| 40 | ); |
| 41 | |
| 42 | return ( |
| 43 | <Card className="h-full w-full rounded-xl shadow-sm"> |
| 44 | <CardContent className="space-y-6 pt-6"> |
| 45 | <div className="space-y-3"> |
| 46 | <div className="flex items-center gap-2"> |
| 47 | <Mail className="h-4 w-4" /> |
| 48 | <span className="font-medium">Primary Email</span> |
| 49 | </div> |
| 50 | <div className="flex items-center justify-between rounded-lg border p-3"> |
| 51 | <span className="text-sm">{primaryEmail}</span> |
| 52 | <Badge variant="secondary">Primary</Badge> |
| 53 | </div> |
| 54 | </div> |
| 55 | |
| 56 | {availableProviders.length > 0 && ( |
| 57 | <div className="space-y-3"> |
| 58 | <div className="flex items-center gap-2"> |
| 59 | <Plus className="h-4 w-4" /> |
| 60 | <span className="font-medium">Link New Account</span> |
| 61 | </div> |
| 62 | <div className="space-y-2"> |
| 63 | {availableProviders.map(provider => ( |
| 64 | <Button |
| 65 | key={provider.id} |
| 66 | variant="outline" |
| 67 | onClick={() => linkMutation.mutate({ provider: provider.id })} |
| 68 | className="w-full justify-start" |
| 69 | > |
| 70 | <span className="mr-2">{provider.icon}</span> |
| 71 | Link {provider.name} Account |
| 72 | </Button> |
| 73 | ))} |
| 74 | </div> |
| 75 | </div> |
| 76 | )} |
| 77 | </CardContent> |
| 78 | </Card> |
| 79 | ); |
| 80 | } |