-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
ExtensionAuthProvider.ts
44 lines (37 loc) · 1.39 KB
/
ExtensionAuthProvider.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import type { AccessToken, AuthProvider, AuthProviderTokenType } from '@twurple/auth';
export class ExtensionAuthProvider implements AuthProvider {
authorizationType = 'Extension';
tokenType: AuthProviderTokenType = 'app';
currentScopes = [];
constructor(public readonly clientId: string) {
if (!('Twitch' in globalThis)) {
throw new Error("This is not an extension, or you didn't load the Twitch Extension Helper properly.");
}
}
async getAccessToken(scopes?: string[]): Promise<AccessToken> {
if (scopes?.length) {
throw new Error(
`Scope ${scopes.join(
', '
)} requested but direct extension calls do not support scopes. Please use an EBS instead.`
);
}
const accessToken = Twitch.ext.viewer.helixToken as string | null;
if (accessToken == null) {
throw new Error(`Could not retrieve an access token from the Twitch extension environment. This could mean different things:
- You're in a mobile extension or the Extension Developer Rig. This is a known issue that has to be resolved by Twitch.
- You didn't wait for the onAuthorized callback before doing any API calls. Before this callback fires, a token is not available.`);
}
return {
accessToken,
refreshToken: null,
expiresIn: null,
obtainmentTimestamp: Date.now(),
scope: []
};
}
async refresh(): Promise<AccessToken> {
// basically just retry
return await this.getAccessToken();
}
}