diff --git a/src/contexts/TwitchServiceContext.tsx b/src/contexts/TwitchServiceContext.tsx
index f08b5fe..0a237d1 100644
--- a/src/contexts/TwitchServiceContext.tsx
+++ b/src/contexts/TwitchServiceContext.tsx
@@ -1,9 +1,11 @@
+import { ResourceComponent } from "@components/ResourceComponent";
import { TwitchApiService } from "@services/twitch/TwitchApiService.ts";
import {
type ParentComponent,
Show,
createContext,
createRenderEffect,
+ createResource,
createSignal,
} from "solid-js";
@@ -13,15 +15,40 @@ const searchParams = new URLSearchParams({
response_type: "token",
scope: "moderator:read:chatters channel:read:subscriptions",
});
-const url = new URL(
+const connectUrl = new URL(
`https://id.twitch.tv/oauth2/authorize?${searchParams.toString()}`,
);
export const TwitchServiceContext = createContext();
export const TwitchServiceContextProvider: ParentComponent = (props) => {
- const [twitchApiService, setTwitchApiService] =
- createSignal();
+ const [twitchApiService, setTwitchApiService] = createSignal<
+ TwitchApiService | undefined
+ >(
+ import.meta.env["VITE_TOKEN_OVERRIDE"]
+ ? new TwitchApiService(import.meta.env["VITE_TOKEN_OVERRIDE"])
+ : undefined,
+ );
+
+ const [resource] = createResource(async () => {
+ if (twitchApiService()) {
+ return;
+ }
+
+ const ls = localStorage.getItem("ACCESS_TOKEN");
+ if (ls) {
+ const response = await fetch("https://id.twitch.tv/oauth2/validate", {
+ headers: {
+ Authorization: `Bearer ${ls}`,
+ },
+ });
+ if (!response.ok) {
+ localStorage.removeItem("ACCESS_TOKEN");
+ return;
+ }
+ setTwitchApiService(new TwitchApiService(ls));
+ }
+ });
createRenderEffect(() => {
const devOverride = import.meta.env["VITE_TOKEN_OVERRIDE"];
@@ -33,24 +60,32 @@ export const TwitchServiceContextProvider: ParentComponent = (props) => {
const urlSearchParams = new URLSearchParams(location.hash.slice(1));
const accessToken = urlSearchParams.get("access_token");
if (accessToken) {
+ localStorage.setItem("ACCESS_TOKEN", accessToken);
setTwitchApiService(new TwitchApiService(accessToken));
window.location.hash = "";
}
}, []);
return (
-
- To use this application, please{" "}
- Connect with Twitch
-
+
+ To use this application, please{" "}
+ Connect with Twitch
+
+ }
+ >
+
+ {props.children}
+
+
}
>
-
- {props.children}
-
-
+ {() => <>>}
+
);
};