-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(providers): Base (very) WIP Spotify provider
- Loading branch information
Showing
15 changed files
with
193 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,6 @@ package-lock.json | |
yarn.lock | ||
|
||
# builds | ||
types | ||
build | ||
*/build | ||
dist | ||
|
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,25 @@ | ||
import z from "zod"; | ||
|
||
const providerConfigs: z.ZodObject<any>[] = []; | ||
|
||
// Import all detected provider config files | ||
const modules: Record<string, z.ZodObject<any>> = import.meta.glob( | ||
"./providers/**/config.ts", | ||
{ | ||
eager: true, | ||
import: "default", | ||
} | ||
); | ||
for (const path in modules) { | ||
const module = modules[path]; | ||
|
||
providerConfigs.push(module); | ||
} | ||
|
||
const envSchema = z.object({}).merge( | ||
providerConfigs.reduce((acc, config) => { | ||
return acc.merge(config); | ||
}, z.object({})) | ||
); | ||
|
||
export default envSchema.parse(import.meta.env); |
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,12 @@ | ||
import { ProviderMeta } from "@/types/providers/meta"; | ||
|
||
const providersGlob = import.meta.glob<ProviderMeta>("./*/index.ts", { | ||
eager: true, | ||
import: "default", | ||
}); | ||
|
||
const providers = Object.fromEntries( | ||
Object.values(providersGlob).map((value) => [value.id, value]) | ||
); | ||
|
||
export default providers; |
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,29 @@ | ||
import { SPOTIFY_OAUTH_SCOPES } from "./constants"; | ||
import { | ||
AccessToken, | ||
SpotifyApi, | ||
AuthorizationCodeWithPKCEStrategy, | ||
} from "@spotify/web-api-ts-sdk"; | ||
|
||
import { providerConfig } from "./config"; | ||
|
||
const { VITE_SPOTIFY_CLIENT_ID, VITE_SPOTIFY_REDIRECT_URI } = providerConfig; | ||
|
||
export async function authenticate(callback: (token: AccessToken) => void) { | ||
const auth = new AuthorizationCodeWithPKCEStrategy( | ||
VITE_SPOTIFY_CLIENT_ID, | ||
VITE_SPOTIFY_REDIRECT_URI, | ||
[...SPOTIFY_OAUTH_SCOPES] | ||
); | ||
const client = new SpotifyApi(auth); | ||
|
||
try { | ||
const { authenticated } = await client.authenticate(); | ||
|
||
if (authenticated) { | ||
console.log("Authenticated"); | ||
} | ||
} catch (e) { | ||
console.error(e); | ||
} | ||
} |
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,10 @@ | ||
import z from "zod"; | ||
|
||
const envSchema = z.object({ | ||
VITE_SPOTIFY_CLIENT_ID: z.string(), | ||
SPOTIFY_CLIENT_SECRET: z.string().optional(), | ||
VITE_SPOTIFY_REDIRECT_URI: z.string(), | ||
}); | ||
export default envSchema; | ||
|
||
export const providerConfig = envSchema.parse(import.meta.env || process.env); |
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,5 @@ | ||
export const SPOTIFY_OAUTH_SCOPES = [ | ||
"user-read-currently-playing", | ||
"user-read-playback-state", | ||
"streaming", | ||
] as const; |
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,8 @@ | ||
import type { ProviderMeta } from "@/types/providers/meta"; | ||
|
||
export default { | ||
name: "Spotify", | ||
id: "spotify", | ||
auth: (await import("./client")).authenticate, | ||
callback: (await import("./client")).authenticate, | ||
} as ProviderMeta; |
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
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,30 @@ | ||
import { createFileRoute, useNavigate } from "@tanstack/react-router"; | ||
|
||
import providers from "@/providers"; | ||
import { useEffect } from "react"; | ||
|
||
export const Route = createFileRoute("/auth/callback/$provider")({ | ||
component: RouteComponent, | ||
}); | ||
|
||
function RouteComponent() { | ||
const { provider } = Route.useParams(); | ||
const navigate = useNavigate(); | ||
|
||
if (!providers[provider]) { | ||
return <h1>Provider not found</h1>; | ||
} | ||
|
||
useEffect(() => { | ||
providers[provider].callback().then(() => { | ||
console.log("Authenticated"); | ||
|
||
navigate({ | ||
to: "/", | ||
replace: true, | ||
}); | ||
}); | ||
}, []); | ||
|
||
return <h1>Authenticating...</h1>; | ||
} |
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
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,6 @@ | ||
export interface ProviderMeta { | ||
name: string; | ||
id: string; | ||
auth: (callback?: (token: any) => void) => any; | ||
callback: (callback?: (token: any) => void) => any; | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.