Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #27 from imyourmanzi/better-login-error-handling
Browse files Browse the repository at this point in the history
Better Login Error Handling
  • Loading branch information
imyourmanzi committed Mar 11, 2023
2 parents 256092e + 2cc88a7 commit ec7b2ae
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "spotify-playlist-manager",
"version": "0.1.5",
"version": "0.1.6",
"license": "MIT",
"scripts": {},
"private": true,
Expand Down
24 changes: 9 additions & 15 deletions packages/ui/src/app/components/home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export const Home = () => {
state: { accessToken, refreshToken },
} = useSpotifyAuth();

const [redirectUri, setRedirectUri] = useState('');
const [hasError, setHasError] = useState(false);

/**
Expand All @@ -32,34 +31,29 @@ export const Home = () => {
}
}, [hasError]);

// get the server to generate the redirect URL, but then place it in the DOM
// reset the error state when dependent resources change
useEffect(() => {
if ((accessToken && refreshToken) || redirectUri) {
if (accessToken && refreshToken) {
return;
}

setHasError(false);
fetch('/api/auth/login')
.then(async (response) => {
const { authRedirect } = await response.json();
setRedirectUri(authRedirect);
})
.catch(() => showErrorToast());
}, [accessToken, refreshToken, redirectUri, showErrorToast]);
}, [accessToken, refreshToken]);

const navigate = useNavigate();

const onLoginInitiate = () => {
if (accessToken && refreshToken) {
navigate('me');
}

if (!redirectUri) {
showErrorToast();
return;
}

window.location.href = redirectUri;
fetch('/api/auth/login')
.then(async (response) => {
const { authRedirect } = await response.json();
window.location.href = authRedirect;
})
.catch(() => showErrorToast());
};

return (
Expand Down

0 comments on commit ec7b2ae

Please sign in to comment.