-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthHandler.tsx
More file actions
53 lines (43 loc) · 1.42 KB
/
AuthHandler.tsx
File metadata and controls
53 lines (43 loc) · 1.42 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
'use client';
import { useEffect } from 'react';
import { useRouter } from 'next/navigation';
import { useAuth } from '@/lib/auth/AuthProvider';
const AuthHandler = () => {
const { user, setUser } = useAuth();
const router = useRouter();
useEffect(() => {
const handleSession = async () => {
console.log('Checking session...');
try {
// Fetch session from our new GitHub auth endpoint instead of Supabase
const response = await fetch('/api/auth/session', {
credentials: 'include',
});
if (!response.ok) {
console.log('No session found. Redirecting to login...');
router.push('/login');
return;
}
const sessionData = await response.json();
if (sessionData.user) {
console.log('User session found:', sessionData.user);
setUser({
userId: sessionData.user.id,
username: sessionData.user.username || sessionData.user.login,
email: sessionData.user.email || '',
role: sessionData.user.role || 'user'
});
} else {
console.log('No user in session. Redirecting to login...');
router.push('/login');
}
} catch (error) {
console.error('Error checking session:', error);
router.push('/login');
}
};
handleSession();
}, [router, setUser]);
return null;
};
export default AuthHandler;