..
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 | import { useEffect, useState } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import axios from 'axios';
import styled from 'styled-components';
const Message = styled.div`
text-align: center;
margin-top: 5rem;
font-size: 1.25rem;
font-weight: bold;
`;
const Callback = () => {
const location = useLocation();
const navigate = useNavigate();
const [errorMessage, setErrorMessage] = useState(''); // State to store the error message
useEffect(() => {
const queryParams = new URLSearchParams(location.search);
const error = queryParams.get('error');
const code = queryParams.get('code');
const PLATFORM = import.meta.env.VITE_DEPLOY_PLATFORM; // This will be set as 'VERCEL' or 'CLOUDFLARE'
// Check if there was an access denied error
if (error === 'access_denied') {
setErrorMessage(
'Authorization revoked. Please click "Authorize" to grant access.',
);
navigate('/callback', { replace: true });
return;
}
// Determine the endpoint based on the platform
const apiEndpoint =
PLATFORM === 'VERCEL' ? '/api/exchange-token' : '/exchange-token';
if (code) {
axios
.post(apiEndpoint, { code })
.then((response) => {
// Store the access token in localStorage
localStorage.setItem('accessToken', response.data.accessToken);
// After setting the token, navigate and force a refresh
navigate('/profile');
window.location.reload(); // Force a full page reload to refresh state
})
.catch((error) => {
const errMsg = error.response?.data?.error || 'Error logging in :(';
console.error('Error in token exchange:', errMsg);
setErrorMessage(errMsg); // Store the error message
navigate('/callback', { replace: true });
});
}
}, [location, navigate]);
return (
<Message>{errorMessage ? `${errorMessage}` : 'Logging in...'}</Message>
);
};
export default Callback;
|
|