-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathLinkedAccounts.tsx
More file actions
371 lines (348 loc) · 11.4 KB
/
Copy pathLinkedAccounts.tsx
File metadata and controls
371 lines (348 loc) · 11.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/**
* LinkedAccounts - Manage Social Recovery & Connected Accounts
*
* Shows all linked providers and allows adding/removing them.
* Linked accounts can be used for:
* - Social recovery if you lose your key
* - Quick login across devices
* - Identity verification
*/
import {
AlertTriangle,
Apple,
Check,
Chrome,
Github,
Loader2,
MessageCircle,
Plus,
Shield,
Trash2,
Twitter,
Wallet,
} from 'lucide-react'
import { AuthProvider } from '@jejunetwork/auth'
import { useJejuAuth, useOAuth3 } from '@jejunetwork/auth/react'
import { useState } from 'react'
type ProviderInfo = {
name: string
icon: React.ComponentType<{ className?: string }>
color: string
description: string
}
type SupportedAuthProvider = Exclude<AuthProvider, 'email' | 'phone'>
const PROVIDER_INFO: Record<SupportedAuthProvider, ProviderInfo> = {
[AuthProvider.WALLET]: {
name: 'Wallet',
icon: Wallet,
color: 'text-orange-400',
description: 'Ethereum wallet',
},
[AuthProvider.GOOGLE]: {
name: 'Google',
icon: Chrome,
color: 'text-red-400',
description: 'Google account',
},
[AuthProvider.APPLE]: {
name: 'Apple',
icon: Apple,
color: 'text-gray-400',
description: 'Apple ID',
},
[AuthProvider.TWITTER]: {
name: 'Twitter',
icon: Twitter,
color: 'text-blue-400',
description: 'Twitter/X account',
},
[AuthProvider.GITHUB]: {
name: 'GitHub',
icon: Github,
color: 'text-purple-400',
description: 'GitHub account',
},
[AuthProvider.DISCORD]: {
name: 'Discord',
icon: MessageCircle,
color: 'text-indigo-400',
description: 'Discord account',
},
[AuthProvider.FARCASTER]: {
name: 'Farcaster',
icon: MessageCircle,
color: 'text-purple-400',
description: 'Farcaster ID',
},
[AuthProvider.PASSKEY]: {
name: 'Passkey',
icon: Shield,
color: 'text-emerald-400',
description: 'Passkey',
},
}
type LinkedProvider = {
provider: SupportedAuthProvider
providerId: string
handle?: string
linkedAt: number
}
const SUPPORTED_PROVIDERS: SupportedAuthProvider[] = [
AuthProvider.WALLET,
AuthProvider.GOOGLE,
AuthProvider.APPLE,
AuthProvider.TWITTER,
AuthProvider.GITHUB,
AuthProvider.DISCORD,
AuthProvider.FARCASTER,
AuthProvider.PASSKEY,
]
function toAuthProvider(type: string): SupportedAuthProvider | null {
switch (type) {
case 'wallet':
return AuthProvider.WALLET
case 'google':
return AuthProvider.GOOGLE
case 'apple':
return AuthProvider.APPLE
case 'twitter':
return AuthProvider.TWITTER
case 'github':
return AuthProvider.GITHUB
case 'discord':
return AuthProvider.DISCORD
case 'farcaster':
return AuthProvider.FARCASTER
case 'passkey':
return AuthProvider.PASSKEY
default:
return null
}
}
export function LinkedAccounts() {
const oauth3 = useOAuth3()
const { linkedAccounts } = useJejuAuth()
const { session, linkProvider, unlinkProvider, isLoading } = oauth3
const [activeAction, setActiveAction] = useState<{
provider: AuthProvider
action: 'link' | 'unlink'
} | null>(null)
const [error, setError] = useState<string | null>(null)
if (!session) {
return (
<div className="rounded-xl bg-card border border-border p-6 text-center">
<p className="text-muted-foreground">
Sign in to manage linked accounts
</p>
</div>
)
}
const linkedProviders = linkedAccounts
.map((account): LinkedProvider | null => {
const provider = toAuthProvider(account.type)
if (!provider) return null
return {
provider,
providerId: account.identifier,
handle: account.handle,
linkedAt: account.linkedAt,
}
})
.filter((account): account is LinkedProvider => account !== null)
const linkedProviderIds = new Set(linkedProviders.map((p) => p.provider))
const availableProviders = SUPPORTED_PROVIDERS.filter(
(provider) => !linkedProviderIds.has(provider),
)
const handleLink = async (provider: AuthProvider) => {
setActiveAction({ provider, action: 'link' })
setError(null)
try {
await linkProvider(provider)
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to link account'
setError(message)
} finally {
setActiveAction(null)
}
}
const handleUnlink = async (provider: AuthProvider) => {
// Don't allow unlinking if it's the only provider
if (linkedProviders.length <= 1) {
setError('Cannot unlink your only recovery method')
return
}
setActiveAction({ provider, action: 'unlink' })
setError(null)
try {
await unlinkProvider(provider)
} catch (err) {
const message = err instanceof Error ? err.message : 'Failed to unlink account'
setError(message)
} finally {
setActiveAction(null)
}
}
return (
<div className="space-y-6">
{/* Header */}
<div className="flex items-center gap-3">
<div className="h-10 w-10 rounded-xl bg-emerald-500/10 flex items-center justify-center">
<Shield className="w-5 h-5 text-emerald-400" />
</div>
<div>
<h3 className="font-semibold">Linked Accounts</h3>
<p className="text-sm text-muted-foreground">
Add social accounts for recovery & easy login
</p>
</div>
</div>
{/* Error */}
{error && (
<div className="p-3 rounded-lg bg-red-500/10 border border-red-500/30 text-red-400 text-sm flex items-center gap-2">
<AlertTriangle className="w-4 h-4 flex-shrink-0" />
{error}
</div>
)}
{/* Recovery Status */}
<div
className={`rounded-xl p-4 border ${
linkedProviders.length >= 2
? 'bg-emerald-500/5 border-emerald-500/30'
: 'bg-yellow-500/5 border-yellow-500/30'
}`}
>
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
{linkedProviders.length >= 2 ? (
<Check className="w-5 h-5 text-emerald-400" />
) : (
<AlertTriangle className="w-5 h-5 text-yellow-400" />
)}
<div>
<p
className={`text-sm font-medium ${linkedProviders.length >= 2 ? 'text-emerald-400' : 'text-yellow-400'}`}
>
{linkedProviders.length >= 2
? 'Social Recovery Enabled'
: 'Add Recovery Options'}
</p>
<p className="text-xs text-muted-foreground">
{linkedProviders.length >= 2
? `${linkedProviders.length} accounts linked for recovery`
: 'Link at least 2 accounts for social recovery'}
</p>
</div>
</div>
<span className="text-2xl font-bold text-muted-foreground">
{linkedProviders.length}/3
</span>
</div>
</div>
{/* Linked Providers */}
{linkedProviders.length > 0 && (
<div className="space-y-2">
<h4 className="text-sm font-medium text-muted-foreground">
Connected
</h4>
<div className="space-y-2">
{linkedProviders.map((linked) => {
const info = PROVIDER_INFO[linked.provider]
const Icon = info.icon
const isActive =
activeAction?.provider === linked.provider &&
activeAction.action === 'unlink'
return (
<div
key={linked.provider}
className="flex items-center justify-between p-3 rounded-xl bg-secondary/30 border border-border"
>
<div className="flex items-center gap-3">
<div
className={`h-10 w-10 rounded-lg bg-secondary flex items-center justify-center`}
>
<Icon className={`w-5 h-5 ${info.color}`} />
</div>
<div>
<p className="font-medium">{info.name}</p>
<p className="text-xs text-muted-foreground">
{linked.handle ||
`Connected ${new Date(linked.linkedAt).toLocaleDateString()}`}
</p>
</div>
</div>
<div className="flex items-center gap-2">
<button
type="button"
onClick={() => handleUnlink(linked.provider)}
disabled={isLoading || linkedProviders.length <= 1}
className="p-2 rounded-lg hover:bg-red-500/10 text-muted-foreground hover:text-red-400 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
title={
linkedProviders.length <= 1
? 'Cannot remove your only recovery method'
: 'Remove'
}
>
{isActive ? (
<Loader2 className="w-4 h-4 animate-spin" />
) : (
<Trash2 className="w-4 h-4" />
)}
</button>
</div>
</div>
)
})}
</div>
</div>
)}
{/* Available Providers */}
{availableProviders.length > 0 && (
<div className="space-y-2">
<h4 className="text-sm font-medium text-muted-foreground">
Add Account
</h4>
<div className="grid grid-cols-2 gap-2">
{availableProviders.map((provider) => {
const info = PROVIDER_INFO[provider]
const Icon = info.icon
const isActive =
activeAction?.provider === provider &&
activeAction.action === 'link'
return (
<button
type="button"
key={provider}
onClick={() => handleLink(provider)}
disabled={isLoading}
className="flex items-center gap-3 p-3 rounded-xl bg-secondary/30 border border-border hover:bg-secondary hover:border-emerald-500/30 transition-all group"
>
<div className="h-8 w-8 rounded-lg bg-secondary flex items-center justify-center group-hover:bg-emerald-500/10">
<Icon className={`w-4 h-4 ${info.color}`} />
</div>
<div className="flex-1 text-left">
<p className="text-sm font-medium">{info.name}</p>
</div>
{isActive ? (
<Loader2 className="w-4 h-4 animate-spin" />
) : (
<Plus className="w-4 h-4 text-muted-foreground opacity-0 group-hover:opacity-100 transition-opacity" />
)}
</button>
)
})}
</div>
</div>
)}
{/* Info */}
<div className="text-xs text-muted-foreground space-y-1">
<p>• Link multiple accounts for social recovery</p>
<p>
• If you lose your wallet key, sign in with any linked account to
recover
</p>
<p>• Your linked accounts are encrypted and stored securely in TEE</p>
</div>
</div>
)
}
export default LinkedAccounts