-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
89 lines (74 loc) · 2.42 KB
/
Copy pathindex.ts
File metadata and controls
89 lines (74 loc) · 2.42 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import type { OAuth2Adapter, OAuth2UserInfo } from "adminforth";
export default class AdminForthAdapterTwitchOauth2 implements OAuth2Adapter {
private clientID: string;
private clientSecret: string;
private redirectUri: string;
constructor(options: {
clientID: string;
clientSecret: string;
redirectUri: string;
}) {
this.clientID = options.clientID;
this.clientSecret = options.clientSecret;
this.redirectUri = options.redirectUri;
}
getAuthUrl(): string {
const params = new URLSearchParams({
client_id: this.clientID,
redirect_uri: this.redirectUri,
response_type: 'code',
scope: 'user:read:email',
force_verify: 'true'
});
return `https://id.twitch.tv/oauth2/authorize?${params.toString()}`;
}
async getTokenFromCode(code: string, redirect_uri: string): Promise<OAuth2UserInfo> {
const tokenRes = await fetch('https://id.twitch.tv/oauth2/token', {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
},
body: new URLSearchParams({
client_id: this.clientID,
client_secret: this.clientSecret,
code,
grant_type: 'authorization_code',
redirect_uri
}),
});
const tokenData = await tokenRes.json();
if (tokenData.error) {
console.error('Token error:', tokenData);
throw new Error(tokenData.error_description || tokenData.error);
}
const accessToken = tokenData.access_token;
const userRes = await fetch('https://api.twitch.tv/helix/users', {
headers: {
'Authorization': `Bearer ${accessToken}`,
'Client-Id': this.clientID
}
});
const userData = await userRes.json();
const user = userData.data?.[0];
if (!user || !user.email) {
throw new Error("Failed to get user email from Twitch");
}
return {
provider: this.constructor.name,
subject: user.id,
email: user.email,
profilePictureUrl: user.profile_image_url,
fullName: user.display_name
};
}
getName(): string {
return 'Twitch';
}
getIcon(): string {
return `<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512">
<path fill="#9146FF" d="M80 0L32 96v352h128v64h64l64-64h96l128-128V0H80zm368 288l-64 64H272l-64 64v-64H96V32h352v256z"/>
<path fill="#9146FF" d="M320 128v96h32v-96h-32zm-80 0v96h32v-96h-32z"/>
</svg>`;
}
}