Skip to content

Commit

Permalink
fix(auth.provider): bugs when redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
joonas-yoon committed Sep 18, 2022
1 parent 97a66e6 commit 0305c22
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
1 change: 0 additions & 1 deletion react-app/src/pages/Auth.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ const CallbackGoogle = () => {
console.log('Recieved data', data);
login({
token: data.access_token,
redirectUrl: '/my',
});
})
.catch(({ response }) => {
Expand Down
36 changes: 23 additions & 13 deletions react-app/src/providers/AuthProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const useAuth = () => {
return value;
};

export const AuthProvider = ({ afterLogin, children }) => {
export const AuthProvider = ({ children }) => {
const navigate = useNavigate();
const [isAuthenticated, setAuthenticated] = useState(false);
const location = useLocation();
Expand All @@ -31,38 +31,47 @@ export const AuthProvider = ({ afterLogin, children }) => {
}
};

const authenticate = async () => {
const access_token = getToken();
const authenticate = async (access_token) => {
if (access_token) {
const result = await customAxios()
const result = await customAxios({
headers: {
access_token,
Authorization: `Bearer ${access_token}`,
},
})
.get('auth/authenticated-route')
.then(() => true)
.catch(() => false);
console.log('authenticate result:', result);
setAuthenticated(result);
updateToken(result ? access_token : null);
} else {
setAuthenticated(false);
updateToken(null);
}
};

useEffect(() => {
authenticate();
console.log('authenticate');
authenticate(getToken());
}, [location]);

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

const handleLogin = ({ token, redirectUrl }) => {
console.log('handleLogin', token);
updateToken(token);
const handleLogin = async ({ token, redirectUrl }) => {
console.log('handleLogin', token, redirectUrl);
await authenticate(token);
const { location } = window;
const url = new URL(location.href);
const next = url.searchParams.get('next');
navigate(redirectUrl || next || afterLogin || '/');
const next = url.searchParams.get('next') || storage.get('login_to', '') || '';
storage.remove('login_to');
navigate(redirectUrl || next || '/');
};

const handleLogout = (options) => {
const handleLogout = async (options) => {
const { redirectUrl } = options || {};
console.log('handleLogout', getToken());
updateToken(null);
setAuthenticated(false);
await authenticate(null);
if (redirectUrl) {
navigate(redirectUrl);
}
Expand All @@ -89,6 +98,7 @@ export const ProtectedRoute = ({ redirectTo, children }) => {
const targetUrl = new URL(redirectTo || '/', origin);
const next = location?.pathname + location?.search + location?.hash;
targetUrl.searchParams.append('next', encodeURI(next));
storage.set('login_to', next);
const nextUrl = targetUrl.toString().replace(targetUrl.origin, '');
return <Navigate to={nextUrl || '/'} replace state={{ from: location }} />;
}
Expand Down

0 comments on commit 0305c22

Please sign in to comment.