-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathDeviceAuthPage.tsx
More file actions
291 lines (274 loc) · 9.56 KB
/
Copy pathDeviceAuthPage.tsx
File metadata and controls
291 lines (274 loc) · 9.56 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
/**
* DeviceAuthPage — /auth/device input + approval surface.
*
* Ported from `framework/apps/account/src/routes/auth.device.tsx`.
* Renders the user-code entry box (when missing from the URL) or the
* approve/deny prompt once the user is signed in. Talks to better-auth's
* device-authorization endpoints directly via fetch.
*/
import { useState } from 'react';
import { Navigate, useLocation, useNavigate, useSearchParams } from 'react-router-dom';
import { CheckCircle2, GalleryVerticalEnd } from 'lucide-react';
import { toast } from 'sonner';
import { useAuth } from '@object-ui/auth';
import { useObjectTranslation } from '@object-ui/i18n';
import {
Button,
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@object-ui/components';
const AUTH_BASE = `${import.meta.env.VITE_SERVER_URL || ''}/api/v1/auth`;
function PageShell({ children }: { children: React.ReactNode }) {
return (
<div className="flex min-h-svh w-full flex-col items-center justify-center gap-6 bg-muted p-6 md:p-10">
<div className="flex w-full max-w-sm flex-col gap-6">
<a href="#" className="flex items-center gap-2 self-center font-medium">
<div className="flex size-6 items-center justify-center rounded-md bg-primary text-primary-foreground">
<GalleryVerticalEnd className="size-4" />
</div>
ObjectStack
</a>
<div className="flex flex-col gap-6">{children}</div>
</div>
</div>
);
}
export function DeviceAuthPage() {
const { t } = useObjectTranslation();
const location = useLocation();
const [params] = useSearchParams();
const code = params.get('user_code') ?? params.get('code') ?? '';
// Device context appended by the requesting runtime's bind/start (ADR
// runtime-identity-binding §2.3). Display-only informed consent — the
// warning copy below is what carries the anti-phishing weight.
const runtimeName = params.get('runtime_name') ?? '';
const runtimeVersion = params.get('runtime_version') ?? '';
const { user, isLoading } = useAuth();
const navigate = useNavigate();
const [submitting, setSubmitting] = useState(false);
const [denying, setDenying] = useState(false);
const [approved, setApproved] = useState(false);
const [denied, setDenied] = useState(false);
const [error, setError] = useState('');
if (!code) {
return (
<PageShell>
<Card>
<CardHeader className="text-center">
<CardTitle>
{t('auth.device.invalidTitle', { defaultValue: 'Invalid device link' })}
</CardTitle>
<CardDescription>
{t('auth.device.invalidDescription', {
defaultValue: 'No device code was provided in the URL.',
})}
</CardDescription>
</CardHeader>
</Card>
</PageShell>
);
}
if (isLoading) {
return (
<PageShell>
<Card>
<CardHeader className="text-center">
<CardDescription>
{t('auth.device.loading', { defaultValue: 'Loading…' })}
</CardDescription>
</CardHeader>
</Card>
</PageShell>
);
}
if (!user) {
// Preserve the FULL query string through the login round-trip — it
// carries the device context (runtime_name / runtime_version) the
// approval card displays, not just the user code.
return (
<Navigate
to={`/login?redirect=${encodeURIComponent(`/auth/device${location.search}`)}`}
replace
/>
);
}
if (approved) {
return (
<PageShell>
<Card>
<CardHeader className="text-center">
<CheckCircle2 className="mx-auto mb-2 h-10 w-10 text-green-500" />
<CardTitle>
{t('auth.device.approvedTitle', { defaultValue: 'Device authorized' })}
</CardTitle>
<CardDescription>
{t('auth.device.approvedDescription', {
defaultValue: 'You can return to the device — it should sign in shortly.',
})}
</CardDescription>
</CardHeader>
</Card>
</PageShell>
);
}
if (denied) {
return (
<PageShell>
<Card>
<CardHeader className="text-center">
<CardTitle>
{t('auth.device.deniedTitle', { defaultValue: 'Access denied' })}
</CardTitle>
<CardDescription>
{t('auth.device.deniedDescription', {
defaultValue: 'The device will not be granted access.',
})}
</CardDescription>
</CardHeader>
</Card>
</PageShell>
);
}
const handleApprove = async () => {
setError('');
setSubmitting(true);
try {
const res = await fetch(`${AUTH_BASE}/device/approve`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify({ userCode: code }),
});
if (!res.ok) {
const data = await res.json().catch(() => ({}));
throw new Error(
(data as { message?: string; error?: { message?: string } })?.message ??
(data as { error?: { message?: string } })?.error?.message ??
t('auth.device.approveFailed', {
defaultValue: 'Approval failed',
}),
);
}
setApproved(true);
toast.success(
t('auth.device.approveSuccess', { defaultValue: 'Device authorized' }),
{
description: t('auth.device.approveSuccessDescription', {
defaultValue: 'You can close this window.',
}),
},
);
} catch (err) {
setError(
(err as Error)?.message ??
t('auth.device.approveFailed', { defaultValue: 'Approval failed' }),
);
} finally {
setSubmitting(false);
}
};
const handleDeny = async () => {
setError('');
setDenying(true);
try {
await fetch(`${AUTH_BASE}/device/deny`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
credentials: 'include',
body: JSON.stringify({ userCode: code }),
});
setDenied(true);
} catch (err) {
setError(
(err as Error)?.message ??
t('auth.device.denyFailed', { defaultValue: 'Failed to deny request' }),
);
} finally {
setDenying(false);
}
};
return (
<PageShell>
<Card>
<CardHeader className="text-center">
<CardTitle>
{t('auth.device.title', { defaultValue: 'Authorize new device' })}
</CardTitle>
<CardDescription>
{t('auth.device.subtitle', {
email: user.email,
defaultValue: `Approve this device to sign in as ${user.email}.`,
})}
</CardDescription>
</CardHeader>
<CardContent className="space-y-4">
{runtimeName ? (
<div className="rounded-md border bg-background px-4 py-3 text-center">
<p className="mb-1 text-xs text-muted-foreground">
{t('auth.device.requesterLabel', { defaultValue: 'Connection request from' })}
</p>
<p className="font-medium">{runtimeName}</p>
{runtimeVersion ? (
<p className="text-xs text-muted-foreground">objectos {runtimeVersion}</p>
) : null}
</div>
) : null}
<div className="rounded-md border bg-background px-4 py-3 text-center">
<p className="mb-1 text-xs text-muted-foreground">
{t('auth.device.userCodeLabel', { defaultValue: 'Device code' })}
</p>
<p className="font-mono text-lg font-semibold tracking-widest">{code}</p>
</div>
<p className="text-center text-xs text-muted-foreground">
{t('auth.device.approveWarning', {
defaultValue:
'Only approve if you started this connection yourself a moment ago. '
+ "Once approved, this runtime can access your organization's private packages.",
})}
</p>
<div className="space-y-4">
<p className="text-center text-sm text-muted-foreground">
{t('auth.device.loggedInAs', {
email: user.email,
defaultValue: `Signed in as ${user.email}`,
})}
</p>
{error && <p className="text-center text-sm text-destructive">{error}</p>}
<Button
onClick={handleApprove}
className="w-full"
disabled={submitting || denying}
>
{submitting
? t('auth.device.approving', { defaultValue: 'Approving…' })
: t('auth.device.approve', { defaultValue: 'Approve device' })}
</Button>
<Button
onClick={handleDeny}
variant="outline"
className="w-full"
disabled={submitting || denying}
>
{denying
? t('auth.device.denying', { defaultValue: 'Denying…' })
: t('auth.device.deny', { defaultValue: 'Deny request' })}
</Button>
<div className="text-center">
<button
type="button"
className="text-sm text-muted-foreground underline-offset-4 hover:underline"
onClick={() => navigate('/')}
>
{t('auth.device.cancel', { defaultValue: 'Cancel' })}
</button>
</div>
</div>
</CardContent>
</Card>
</PageShell>
);
}
export default DeviceAuthPage;