-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplify login and make tracks selectable
- Loading branch information
Showing
8 changed files
with
241 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { IRedirectionStrategy, SpotifyApi } from "@spotify/web-api-ts-sdk" | ||
import { useEffect, useState } from "preact/hooks" | ||
import { createApiClient } from "./api" | ||
import App from "./app/app" | ||
import Home from "./home" | ||
|
||
export default function Main() { | ||
const [spotify, setSpotify] = useState<SpotifyApi>(null) | ||
const [authError, setAuthError] = useState<string>(null) | ||
|
||
useEffect(() => { | ||
// check if user is already authenticated | ||
;(async () => { | ||
try { | ||
// do not redirect here, since this means user is not authenticated, do nothing instead | ||
class NoRedirectionStrategy implements IRedirectionStrategy { | ||
public async redirect(_targetUrl: string | URL): Promise<void> { | ||
return | ||
} | ||
public async onReturnFromRedirect(): Promise<void> {} | ||
} | ||
const sdk = createApiClient(new NoRedirectionStrategy()) | ||
const { authenticated } = await sdk.authenticate() | ||
if (authenticated && !spotify) { | ||
setSpotify(sdk) | ||
} | ||
} catch (e) { | ||
console.info("User is not authenticated", e) | ||
} | ||
})() | ||
}, []) | ||
|
||
if (spotify) { | ||
return ( | ||
<App | ||
onLogout={() => { | ||
spotify.logOut() | ||
setSpotify(null) | ||
}} | ||
spotify={spotify} | ||
/> | ||
) | ||
} | ||
|
||
return ( | ||
<Home | ||
authError={authError} | ||
onLogin={async () => { | ||
try { | ||
const sdk = createApiClient() | ||
const { authenticated } = await sdk.authenticate() | ||
if (authenticated) { | ||
// this should never be reached since authenticate redirects | ||
setSpotify(sdk) | ||
} | ||
} catch (e: Error | unknown) { | ||
console.error(e) | ||
setAuthError(e.toString() || "") | ||
} | ||
}} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.