-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathAuthCallbackContent.tsx
More file actions
86 lines (74 loc) · 2.28 KB
/
AuthCallbackContent.tsx
File metadata and controls
86 lines (74 loc) · 2.28 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
'use client';
import { useRouter, useSearchParams } from 'next/navigation';
import { useEffect } from 'react';
import styled from 'styled-components';
import { setCredentials } from '@/features/auth/authSlice';
import { useAppDispatch } from '@/redux/hooks';
import type { UserInfo } from '@/types/user';
const Container = styled.div`
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #fafafa;
`;
const LoadingCard = styled.div`
background: white;
padding: 40px;
border-radius: 12px;
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
text-align: center;
`;
const LoadingText = styled.p`
font-size: 18px;
color: #666;
margin: 0;
`;
export default function AuthCallbackContent() {
const router = useRouter();
const searchParams = useSearchParams();
const dispatch = useAppDispatch();
useEffect(() => {
// Now we expect csrfToken and user (JWT token is in httpOnly cookie)
const csrfToken = searchParams.get('csrfToken');
const userString = searchParams.get('user');
if (csrfToken && userString) {
try {
const parsedUser = JSON.parse(
decodeURIComponent(userString),
) as UserInfo;
// Clear any persisted auth state to prevent old user ID from being used
localStorage.removeItem('persist:root');
dispatch(
setCredentials({
csrfToken,
user: {
_id: parsedUser._id,
email: parsedUser.email,
firstName: parsedUser.firstName,
lastName: parsedUser.lastName,
role: parsedUser.role,
status: parsedUser.status,
},
}),
);
router.replace('/admin/overview');
} catch (error) {
// eslint-disable-next-line no-console
console.error('Auth callback - parsing error:', error);
router.replace('/login?error=oauth_error');
}
} else {
// eslint-disable-next-line no-console
console.error('Auth callback - missing csrfToken or userString');
router.replace('/login?error=oauth_error');
}
}, [searchParams, dispatch, router]);
return (
<Container>
<LoadingCard>
<LoadingText>Completing authentication...</LoadingText>
</LoadingCard>
</Container>
);
}