Skip to content

Commit 0305c22

Browse files
committed
fix(auth.provider): bugs when redirect
1 parent 97a66e6 commit 0305c22

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

react-app/src/pages/Auth.jsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@ const CallbackGoogle = () => {
149149
console.log('Recieved data', data);
150150
login({
151151
token: data.access_token,
152-
redirectUrl: '/my',
153152
});
154153
})
155154
.catch(({ response }) => {

react-app/src/providers/AuthProvider.jsx

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const useAuth = () => {
1414
return value;
1515
};
1616

17-
export const AuthProvider = ({ afterLogin, children }) => {
17+
export const AuthProvider = ({ children }) => {
1818
const navigate = useNavigate();
1919
const [isAuthenticated, setAuthenticated] = useState(false);
2020
const location = useLocation();
@@ -31,38 +31,47 @@ export const AuthProvider = ({ afterLogin, children }) => {
3131
}
3232
};
3333

34-
const authenticate = async () => {
35-
const access_token = getToken();
34+
const authenticate = async (access_token) => {
3635
if (access_token) {
37-
const result = await customAxios()
36+
const result = await customAxios({
37+
headers: {
38+
access_token,
39+
Authorization: `Bearer ${access_token}`,
40+
},
41+
})
3842
.get('auth/authenticated-route')
3943
.then(() => true)
4044
.catch(() => false);
45+
console.log('authenticate result:', result);
4146
setAuthenticated(result);
4247
updateToken(result ? access_token : null);
48+
} else {
49+
setAuthenticated(false);
50+
updateToken(null);
4351
}
4452
};
4553

4654
useEffect(() => {
47-
authenticate();
55+
console.log('authenticate');
56+
authenticate(getToken());
4857
}, [location]);
4958

5059
console.log('[Provider] token:', getToken());
5160

52-
const handleLogin = ({ token, redirectUrl }) => {
53-
console.log('handleLogin', token);
54-
updateToken(token);
61+
const handleLogin = async ({ token, redirectUrl }) => {
62+
console.log('handleLogin', token, redirectUrl);
63+
await authenticate(token);
5564
const { location } = window;
5665
const url = new URL(location.href);
57-
const next = url.searchParams.get('next');
58-
navigate(redirectUrl || next || afterLogin || '/');
66+
const next = url.searchParams.get('next') || storage.get('login_to', '') || '';
67+
storage.remove('login_to');
68+
navigate(redirectUrl || next || '/');
5969
};
6070

61-
const handleLogout = (options) => {
71+
const handleLogout = async (options) => {
6272
const { redirectUrl } = options || {};
6373
console.log('handleLogout', getToken());
64-
updateToken(null);
65-
setAuthenticated(false);
74+
await authenticate(null);
6675
if (redirectUrl) {
6776
navigate(redirectUrl);
6877
}
@@ -89,6 +98,7 @@ export const ProtectedRoute = ({ redirectTo, children }) => {
8998
const targetUrl = new URL(redirectTo || '/', origin);
9099
const next = location?.pathname + location?.search + location?.hash;
91100
targetUrl.searchParams.append('next', encodeURI(next));
101+
storage.set('login_to', next);
92102
const nextUrl = targetUrl.toString().replace(targetUrl.origin, '');
93103
return <Navigate to={nextUrl || '/'} replace state={{ from: location }} />;
94104
}

0 commit comments

Comments
 (0)