-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpage.tsx
More file actions
153 lines (139 loc) · 4.94 KB
/
page.tsx
File metadata and controls
153 lines (139 loc) · 4.94 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
'use client';
import { useEffect, useState, Suspense } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from '@/components/ui/card';
import { Button } from '@/components/ui/button';
import { CheckCircle, XCircle, Loader2 } from 'lucide-react';
function AuthCallbackContent() {
const router = useRouter();
const searchParams = useSearchParams();
const [status, setStatus] = useState<'loading' | 'success' | 'error'>(
'loading'
);
const [errorMessage, setErrorMessage] = useState('');
const API_BASE = process.env.NEXT_PUBLIC_API_BASE || 'http://localhost:8000/api';
useEffect(() => {
const handleCallback = async () => {
const code = searchParams.get('code');
const state = searchParams.get('state');
const error = searchParams.get('error');
if (error) {
setStatus('error');
setErrorMessage(`Authentication failed: ${error}`);
return;
}
if (!code || !state) {
setStatus('error');
setErrorMessage('Missing authentication parameters');
return;
}
try {
const response = await fetch(
`${API_BASE}/admin/auth/callback?code=${encodeURIComponent(
code
)}&state=${encodeURIComponent(state)}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
credentials: 'include',
}
);
if (response.ok) {
setStatus('success');
// Redirect to admin panel after a short delay
setTimeout(() => {
router.push('/admin');
}, 2000);
} else {
const errorData = await response.json();
throw new Error(errorData.detail || 'Authentication failed');
}
} catch (error) {
console.error('Auth callback error:', error);
setStatus('error');
setErrorMessage(error.message || 'Authentication failed');
}
};
handleCallback();
}, [searchParams, router]);
return (
<div className="min-h-screen flex items-center justify-center bg-background">
<Card className="w-full max-w-md shadow-lg">
<CardHeader className="text-center space-y-4">
<div className="mx-auto w-12 h-12 rounded-full flex items-center justify-center">
{status === 'loading' && (
<div className="w-12 h-12 bg-primary/10 rounded-full flex items-center justify-center">
<Loader2 className="w-6 h-6 text-primary animate-spin" />
</div>
)}
{status === 'success' && (
<div className="w-12 h-12 bg-green-500/10 rounded-full flex items-center justify-center">
<CheckCircle className="w-6 h-6 text-green-600" />
</div>
)}
{status === 'error' && (
<div className="w-12 h-12 bg-red-500/10 rounded-full flex items-center justify-center">
<XCircle className="w-6 h-6 text-red-600" />
</div>
)}
</div>
<div>
<CardTitle className="text-2xl">
{status === 'loading' && 'Authenticating...'}
{status === 'success' && 'Authentication Successful'}
{status === 'error' && 'Authentication Failed'}
</CardTitle>
<CardDescription className="mt-2">
{status === 'loading' &&
'Please wait while we verify your GitHub credentials'}
{status === 'success' && 'Redirecting to admin panel...'}
{status === 'error' && errorMessage}
</CardDescription>
</div>
</CardHeader>
{status === 'error' && (
<CardContent>
<Button onClick={() => router.push('/admin')} className="w-full">
Try Again
</Button>
</CardContent>
)}
</Card>
</div>
);
}
export default function AuthCallbackPage() {
return (
<Suspense
fallback={
<div className="min-h-screen flex items-center justify-center bg-background">
<Card className="w-full max-w-md shadow-lg">
<CardHeader className="text-center space-y-4">
<div className="mx-auto w-12 h-12 rounded-full flex items-center justify-center">
<div className="w-12 h-12 bg-primary/10 rounded-full flex items-center justify-center">
<Loader2 className="w-6 h-6 text-primary animate-spin" />
</div>
</div>
<div>
<CardTitle className="text-2xl">Loading...</CardTitle>
<CardDescription className="mt-2">
Please wait while we load the authentication page
</CardDescription>
</div>
</CardHeader>
</Card>
</div>
}
>
<AuthCallbackContent />
</Suspense>
);
}