|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { Icons } from '@comp/ui/icons'; |
| 4 | +import { Card, CardContent, CardHeader, CardTitle } from '@comp/ui/card'; |
| 5 | +import { Loader2, CheckCircle2 } from 'lucide-react'; |
| 6 | +import { useSearchParams } from 'next/navigation'; |
| 7 | +import { useEffect, useState } from 'react'; |
| 8 | + |
| 9 | +type Status = 'redirecting' | 'success' | 'error'; |
| 10 | + |
| 11 | +export default function DeviceCallbackPage() { |
| 12 | + const searchParams = useSearchParams(); |
| 13 | + const [status, setStatus] = useState<Status>('redirecting'); |
| 14 | + const [errorMessage, setErrorMessage] = useState(''); |
| 15 | + |
| 16 | + useEffect(() => { |
| 17 | + const callbackPort = searchParams.get('callback_port'); |
| 18 | + const state = searchParams.get('state'); |
| 19 | + |
| 20 | + if (!callbackPort || !state) { |
| 21 | + setStatus('error'); |
| 22 | + setErrorMessage('Missing required parameters. Please try signing in again from the Comp AI agent.'); |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + const port = Number.parseInt(callbackPort, 10); |
| 27 | + if (Number.isNaN(port) || port < 1 || port > 65535) { |
| 28 | + setStatus('error'); |
| 29 | + setErrorMessage('Invalid callback port. Please try signing in again from the Comp AI agent.'); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + async function exchangeAndRedirect() { |
| 34 | + try { |
| 35 | + // Generate an auth code using the authenticated session |
| 36 | + const response = await fetch('/api/device-agent/auth-code', { |
| 37 | + method: 'POST', |
| 38 | + headers: { 'Content-Type': 'application/json' }, |
| 39 | + body: JSON.stringify({ callback_port: port, state }), |
| 40 | + }); |
| 41 | + |
| 42 | + if (!response.ok) { |
| 43 | + const data = await response.json().catch(() => ({})); |
| 44 | + throw new Error(data.error || `Server returned ${response.status}`); |
| 45 | + } |
| 46 | + |
| 47 | + const { code } = await response.json(); |
| 48 | + |
| 49 | + // Redirect to the device agent's localhost server |
| 50 | + window.location.href = `http://localhost:${port}/auth-callback?code=${code}&state=${state}`; |
| 51 | + setStatus('success'); |
| 52 | + } catch (err) { |
| 53 | + console.error('Device auth callback failed:', err); |
| 54 | + setStatus('error'); |
| 55 | + setErrorMessage( |
| 56 | + err instanceof Error |
| 57 | + ? err.message |
| 58 | + : 'Failed to complete sign-in. Please try again from the Comp AI agent.', |
| 59 | + ); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + exchangeAndRedirect(); |
| 64 | + }, [searchParams]); |
| 65 | + |
| 66 | + return ( |
| 67 | + <div className="flex min-h-dvh flex-col text-foreground"> |
| 68 | + <main className="flex flex-1 items-center justify-center p-6"> |
| 69 | + <Card className="w-full max-w-md"> |
| 70 | + <CardHeader className="text-center space-y-3 pt-10"> |
| 71 | + <Icons.Logo className="h-10 w-10 mx-auto" /> |
| 72 | + <CardTitle className="text-xl tracking-tight text-card-foreground"> |
| 73 | + {status === 'redirecting' && 'Completing sign-in...'} |
| 74 | + {status === 'success' && 'Sign-in complete!'} |
| 75 | + {status === 'error' && 'Sign-in failed'} |
| 76 | + </CardTitle> |
| 77 | + </CardHeader> |
| 78 | + <CardContent className="text-center pb-10"> |
| 79 | + {status === 'redirecting' && ( |
| 80 | + <div className="flex flex-col items-center gap-3"> |
| 81 | + <Loader2 className="h-6 w-6 animate-spin text-muted-foreground" /> |
| 82 | + <p className="text-sm text-muted-foreground"> |
| 83 | + Redirecting to the Comp AI agent... |
| 84 | + </p> |
| 85 | + </div> |
| 86 | + )} |
| 87 | + {status === 'success' && ( |
| 88 | + <div className="flex flex-col items-center gap-3"> |
| 89 | + <CheckCircle2 className="h-6 w-6 text-green-500" /> |
| 90 | + <p className="text-sm text-muted-foreground"> |
| 91 | + You can close this tab and return to the Comp AI agent. |
| 92 | + </p> |
| 93 | + </div> |
| 94 | + )} |
| 95 | + {status === 'error' && ( |
| 96 | + <p className="text-sm text-destructive"> |
| 97 | + {errorMessage} |
| 98 | + </p> |
| 99 | + )} |
| 100 | + </CardContent> |
| 101 | + </Card> |
| 102 | + </main> |
| 103 | + </div> |
| 104 | + ); |
| 105 | +} |
0 commit comments