|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +import { createFileRoute, useNavigate } from '@tanstack/react-router'; |
| 4 | +import { useState } from 'react'; |
| 5 | +import { useClient } from '@objectstack/client-react'; |
| 6 | +import { Button } from '@/components/ui/button'; |
| 7 | +import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; |
| 8 | +import { Input } from '@/components/ui/input'; |
| 9 | +import { Label } from '@/components/ui/label'; |
| 10 | +import { toast } from '@/hooks/use-toast'; |
| 11 | +import { useSession } from '@/hooks/useSession'; |
| 12 | +import { CheckCircle2, Monitor, AlertCircle } from 'lucide-react'; |
| 13 | + |
| 14 | +export const Route = createFileRoute('/auth/device')({ |
| 15 | + validateSearch: (search: Record<string, unknown>) => ({ |
| 16 | + code: (search.code as string) ?? '', |
| 17 | + }), |
| 18 | + component: DeviceAuthPage, |
| 19 | +}); |
| 20 | + |
| 21 | +function DeviceAuthPage() { |
| 22 | + const { code } = Route.useSearch(); |
| 23 | + const { user, refresh } = useSession(); |
| 24 | + const client = useClient() as any; |
| 25 | + const navigate = useNavigate(); |
| 26 | + |
| 27 | + const [email, setEmail] = useState(''); |
| 28 | + const [password, setPassword] = useState(''); |
| 29 | + const [submitting, setSubmitting] = useState(false); |
| 30 | + const [approved, setApproved] = useState(false); |
| 31 | + const [error, setError] = useState(''); |
| 32 | + |
| 33 | + if (!code) { |
| 34 | + return ( |
| 35 | + <div className="min-h-screen flex items-center justify-center bg-background"> |
| 36 | + <Card className="w-full max-w-md"> |
| 37 | + <CardHeader className="text-center"> |
| 38 | + <AlertCircle className="h-12 w-12 text-destructive mx-auto mb-2" /> |
| 39 | + <CardTitle>Invalid Request</CardTitle> |
| 40 | + <CardDescription>No device code provided.</CardDescription> |
| 41 | + </CardHeader> |
| 42 | + </Card> |
| 43 | + </div> |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + if (approved) { |
| 48 | + return ( |
| 49 | + <div className="min-h-screen flex items-center justify-center bg-background"> |
| 50 | + <Card className="w-full max-w-md"> |
| 51 | + <CardHeader className="text-center"> |
| 52 | + <CheckCircle2 className="h-12 w-12 text-green-500 mx-auto mb-2" /> |
| 53 | + <CardTitle>Login Approved</CardTitle> |
| 54 | + <CardDescription> |
| 55 | + You can close this tab. The CLI has been authenticated successfully. |
| 56 | + </CardDescription> |
| 57 | + </CardHeader> |
| 58 | + </Card> |
| 59 | + </div> |
| 60 | + ); |
| 61 | + } |
| 62 | + |
| 63 | + const handleLogin = async (e: React.FormEvent) => { |
| 64 | + e.preventDefault(); |
| 65 | + setError(''); |
| 66 | + setSubmitting(true); |
| 67 | + try { |
| 68 | + await client.auth.login({ type: 'email', email, password }); |
| 69 | + await refresh(); |
| 70 | + } catch (err: any) { |
| 71 | + setError(err?.message ?? 'Login failed'); |
| 72 | + } finally { |
| 73 | + setSubmitting(false); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + const handleApprove = async () => { |
| 78 | + setError(''); |
| 79 | + setSubmitting(true); |
| 80 | + try { |
| 81 | + // Get the current session token from better-auth cookie/session |
| 82 | + const sessionRes = await fetch('/api/v1/auth/get-session', { credentials: 'include' }); |
| 83 | + const sessionData = await sessionRes.json() as any; |
| 84 | + const token = sessionData?.session?.token; |
| 85 | + if (!token) throw new Error('No active session'); |
| 86 | + |
| 87 | + const res = await fetch('/api/v1/auth/device/approve', { |
| 88 | + method: 'POST', |
| 89 | + headers: { 'Content-Type': 'application/json' }, |
| 90 | + credentials: 'include', |
| 91 | + body: JSON.stringify({ code, token }), |
| 92 | + }); |
| 93 | + const data = await res.json() as any; |
| 94 | + if (!data.success) throw new Error(data.error?.message ?? 'Approval failed'); |
| 95 | + |
| 96 | + setApproved(true); |
| 97 | + toast({ title: 'CLI login approved', description: 'The CLI has been authenticated.' }); |
| 98 | + } catch (err: any) { |
| 99 | + setError(err?.message ?? 'Approval failed'); |
| 100 | + } finally { |
| 101 | + setSubmitting(false); |
| 102 | + } |
| 103 | + }; |
| 104 | + |
| 105 | + return ( |
| 106 | + <div className="min-h-screen flex items-center justify-center bg-background p-4"> |
| 107 | + <Card className="w-full max-w-md"> |
| 108 | + <CardHeader className="text-center"> |
| 109 | + <Monitor className="h-10 w-10 text-primary mx-auto mb-2" /> |
| 110 | + <CardTitle>CLI Login Request</CardTitle> |
| 111 | + <CardDescription> |
| 112 | + {user |
| 113 | + ? `Approve CLI access for ${user.email}` |
| 114 | + : 'Sign in to approve the CLI login request'} |
| 115 | + </CardDescription> |
| 116 | + </CardHeader> |
| 117 | + <CardContent className="space-y-4"> |
| 118 | + <div className="bg-muted rounded-md px-4 py-2 text-center"> |
| 119 | + <p className="text-xs text-muted-foreground mb-1">Device Code</p> |
| 120 | + <p className="font-mono font-semibold tracking-widest text-lg">{code}</p> |
| 121 | + </div> |
| 122 | + |
| 123 | + {!user ? ( |
| 124 | + <form onSubmit={handleLogin} className="space-y-4"> |
| 125 | + <div className="space-y-2"> |
| 126 | + <Label htmlFor="email">Email</Label> |
| 127 | + <Input |
| 128 | + id="email" |
| 129 | + type="email" |
| 130 | + placeholder="you@example.com" |
| 131 | + value={email} |
| 132 | + onChange={(e) => setEmail(e.target.value)} |
| 133 | + required |
| 134 | + autoFocus |
| 135 | + /> |
| 136 | + </div> |
| 137 | + <div className="space-y-2"> |
| 138 | + <Label htmlFor="password">Password</Label> |
| 139 | + <Input |
| 140 | + id="password" |
| 141 | + type="password" |
| 142 | + value={password} |
| 143 | + onChange={(e) => setPassword(e.target.value)} |
| 144 | + required |
| 145 | + /> |
| 146 | + </div> |
| 147 | + {error && <p className="text-sm text-destructive">{error}</p>} |
| 148 | + <Button type="submit" className="w-full" disabled={submitting}> |
| 149 | + {submitting ? 'Signing in…' : 'Sign In'} |
| 150 | + </Button> |
| 151 | + </form> |
| 152 | + ) : ( |
| 153 | + <div className="space-y-4"> |
| 154 | + <p className="text-sm text-center text-muted-foreground"> |
| 155 | + Logged in as <span className="font-medium text-foreground">{user.email}</span> |
| 156 | + </p> |
| 157 | + {error && <p className="text-sm text-destructive text-center">{error}</p>} |
| 158 | + <Button onClick={handleApprove} className="w-full" disabled={submitting}> |
| 159 | + {submitting ? 'Approving…' : 'Approve CLI Access'} |
| 160 | + </Button> |
| 161 | + <Button |
| 162 | + variant="outline" |
| 163 | + className="w-full" |
| 164 | + onClick={() => navigate({ to: '/' })} |
| 165 | + > |
| 166 | + Cancel |
| 167 | + </Button> |
| 168 | + </div> |
| 169 | + )} |
| 170 | + </CardContent> |
| 171 | + </Card> |
| 172 | + </div> |
| 173 | + ); |
| 174 | +} |
0 commit comments