stay signed in
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
|
import { ResourceComponent } from "@components/ResourceComponent";
|
||||||
import { TwitchApiService } from "@services/twitch/TwitchApiService.ts";
|
import { TwitchApiService } from "@services/twitch/TwitchApiService.ts";
|
||||||
import {
|
import {
|
||||||
type ParentComponent,
|
type ParentComponent,
|
||||||
Show,
|
Show,
|
||||||
createContext,
|
createContext,
|
||||||
createRenderEffect,
|
createRenderEffect,
|
||||||
|
createResource,
|
||||||
createSignal,
|
createSignal,
|
||||||
} from "solid-js";
|
} from "solid-js";
|
||||||
|
|
||||||
@@ -13,15 +15,40 @@ const searchParams = new URLSearchParams({
|
|||||||
response_type: "token",
|
response_type: "token",
|
||||||
scope: "moderator:read:chatters channel:read:subscriptions",
|
scope: "moderator:read:chatters channel:read:subscriptions",
|
||||||
});
|
});
|
||||||
const url = new URL(
|
const connectUrl = new URL(
|
||||||
`https://id.twitch.tv/oauth2/authorize?${searchParams.toString()}`,
|
`https://id.twitch.tv/oauth2/authorize?${searchParams.toString()}`,
|
||||||
);
|
);
|
||||||
|
|
||||||
export const TwitchServiceContext = createContext<TwitchApiService>();
|
export const TwitchServiceContext = createContext<TwitchApiService>();
|
||||||
|
|
||||||
export const TwitchServiceContextProvider: ParentComponent = (props) => {
|
export const TwitchServiceContextProvider: ParentComponent = (props) => {
|
||||||
const [twitchApiService, setTwitchApiService] =
|
const [twitchApiService, setTwitchApiService] = createSignal<
|
||||||
createSignal<TwitchApiService>();
|
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(() => {
|
createRenderEffect(() => {
|
||||||
const devOverride = import.meta.env["VITE_TOKEN_OVERRIDE"];
|
const devOverride = import.meta.env["VITE_TOKEN_OVERRIDE"];
|
||||||
@@ -33,18 +60,22 @@ export const TwitchServiceContextProvider: ParentComponent = (props) => {
|
|||||||
const urlSearchParams = new URLSearchParams(location.hash.slice(1));
|
const urlSearchParams = new URLSearchParams(location.hash.slice(1));
|
||||||
const accessToken = urlSearchParams.get("access_token");
|
const accessToken = urlSearchParams.get("access_token");
|
||||||
if (accessToken) {
|
if (accessToken) {
|
||||||
|
localStorage.setItem("ACCESS_TOKEN", accessToken);
|
||||||
setTwitchApiService(new TwitchApiService(accessToken));
|
setTwitchApiService(new TwitchApiService(accessToken));
|
||||||
window.location.hash = "";
|
window.location.hash = "";
|
||||||
}
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
<ResourceComponent
|
||||||
|
resource={resource}
|
||||||
|
fallback={
|
||||||
<Show
|
<Show
|
||||||
when={twitchApiService()}
|
when={twitchApiService()}
|
||||||
fallback={
|
fallback={
|
||||||
<p>
|
<p>
|
||||||
To use this application, please{" "}
|
To use this application, please{" "}
|
||||||
<a href={url.toString()}>Connect with Twitch</a>
|
<a href={connectUrl.toString()}>Connect with Twitch</a>
|
||||||
</p>
|
</p>
|
||||||
}
|
}
|
||||||
>
|
>
|
||||||
@@ -52,5 +83,9 @@ export const TwitchServiceContextProvider: ParentComponent = (props) => {
|
|||||||
{props.children}
|
{props.children}
|
||||||
</TwitchServiceContext.Provider>
|
</TwitchServiceContext.Provider>
|
||||||
</Show>
|
</Show>
|
||||||
|
}
|
||||||
|
>
|
||||||
|
{() => <></>}
|
||||||
|
</ResourceComponent>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user