Skip to content

Commit

Permalink
Fix login infinite loop
Browse files Browse the repository at this point in the history
  • Loading branch information
umonkey committed Apr 7, 2024
1 parent d2c0bd7 commit 62bd4f2
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export const SelectLocationDialog = (props: IProps) => {

const handleLoginSuccess = () => {
console.debug("Login successful.");

// For some reason, userInfo is not updated immediately after login,
// so we need to reload the page to get the updated userInfo.
window.location.reload();
};

const handleLoginError = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ export const LoginWithGoogleButton = (props: IProps) => {
};

return (
<Button variant="contained" color="success" disabled={props.disabled} onClick={handleClick}>Continue</Button>
<Button variant="contained" color="success" disabled={props.disabled} onClick={handleClick}>Log In with Google</Button>
);
};
40 changes: 16 additions & 24 deletions frontend/src/components/elements/LoginWithGoogleButton/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,22 @@ export const useGoogleAuth = (props: IProps) => {
const { setUserInfo } = useUserInfo();

const loginFunction = useGoogleLogin({
onSuccess: (response) => {
console.debug("Received a token from Google.");
setToken(response);
onSuccess: async (response) => {
console.debug("Google auth successful, received access token.");

const token = response.access_token;

try {
const res = await treeMapService.loginGoogle(token);
console.info("Logged in with Google.");

setUserInfo(res);

props.onSuccess();
} catch (e) {
console.error("Error logging in with Google:", e);
props.onError();
}
},

onError: (error) => {
Expand All @@ -26,27 +39,6 @@ export const useGoogleAuth = (props: IProps) => {
},
});

// When a Google token is received, exchange it for user info.
useEffect(() => {
(async () => {
if (token) {
console.debug("Access token received from Google. Logging in.");

try {
const res = await treeMapService.loginGoogle(token.access_token);

setUserInfo(res);
console.info("Logged in with Google.");

props.onSuccess();
} catch (e) {
console.error("Error logging in with Google:", e);
props.onError();
}
}
})();
}, [token, setUserInfo, props]);

return {
login: () => { loginFunction() },
};
Expand Down
12 changes: 10 additions & 2 deletions frontend/src/utils/userinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const useUserInfo = () => {
const [userInfo, setUserInfo] = useState<IUserInfo | null>(readStoredValue());

const handleChange = useCallback((event: StorageEvent) => {
console.debug("CHANGE", event);

if (event.key === USER_INFO_KEY) {
console.debug("User info change detected.");
setUserInfo(JSON.parse(event.newValue || "null"));
Expand All @@ -37,14 +39,20 @@ export const useUserInfo = () => {
}, [handleChange]);

// Modify the stored value.
// The storage event is only triggered when the value is changed in another tab.
// So we need to also trigger the change manually.
const setter = (value: IUserInfo | null) => {
try {
if (value) {
localStorage.setItem(USER_INFO_KEY, JSON.stringify(value));
console.debug("User info updated.");
console.debug("User info updated in localStorage.");

setUserInfo(value);
} else {
localStorage.removeItem(USER_INFO_KEY);
console.debug("User info removed.");
console.debug("User info removed from localStorage.");

setUserInfo(null);
}
} catch (e) {
console.error("Error updating user info.", e);
Expand Down

0 comments on commit 62bd4f2

Please sign in to comment.