forked from worlddriven/webapp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
86 lines (74 loc) · 1.96 KB
/
index.jsx
File metadata and controls
86 lines (74 loc) · 1.96 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
import { useEffect, useState } from 'react';
import { useNavigate, useSearchParams } from 'react-router-dom';
import styled from 'styled-components';
const Container = styled.div`
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 50vh;
text-align: center;
`;
const Message = styled.p`
font-size: 1.2rem;
color: var(--color-text);
`;
const ErrorMessage = styled.p`
font-size: 1.2rem;
color: #dc3545;
`;
export function AuthCallback() {
const [searchParams] = useSearchParams();
const navigate = useNavigate();
const [error, setError] = useState(null);
useEffect(() => {
async function handleCallback() {
const code = searchParams.get('code');
const oauthError = searchParams.get('error');
if (oauthError) {
setError('Authentication was denied');
return;
}
if (!code) {
setError('No authorization code received');
return;
}
try {
const callbackUrl = `${window.location.origin}/auth/callback`;
const response = await fetch('/api/auth/callback', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
code,
redirect_uri: callbackUrl,
}),
});
if (response.ok) {
navigate('/dashboard');
} else {
const data = await response.json().catch(() => ({}));
setError(data.error || 'Authentication failed');
}
} catch (err) {
console.error('Auth callback error:', err);
setError('Authentication failed');
}
}
handleCallback();
}, [searchParams, navigate]);
if (error) {
return (
<Container>
<ErrorMessage>{error}</ErrorMessage>
<a href="/">Return to home</a>
</Container>
);
}
return (
<Container>
<Message>Completing authentication...</Message>
</Container>
);
}