-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathauthFlow.ts
134 lines (119 loc) · 3.24 KB
/
authFlow.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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { HTTPException } from 'hono/http-exception'
import type { Token } from '../../types'
import { toQueryParams } from '../../utils/objectToQuery'
import type {
TwitchErrorResponse,
TwitchUserResponse,
TwitchTokenResponse,
TwitchUser,
Scopes,
} from './types'
type TwitchAuthFlow = {
client_id: string
client_secret: string
redirect_uri: string
scope: Scopes[]
state: string
code: string | undefined
force_verify: boolean | undefined
// token: Token | undefined
}
export class AuthFlow {
client_id: string
client_secret: string
redirect_uri: string
scope: string
state: string
code: string | undefined
token: Token | undefined
refresh_token: Token | undefined
granted_scopes: string[] | undefined
user: Partial<TwitchUser> | undefined
force_verify: boolean | undefined
constructor({
client_id,
client_secret,
redirect_uri,
scope,
state,
code,
force_verify,
// token,
}: TwitchAuthFlow) {
this.client_id = client_id
this.client_secret = client_secret
this.redirect_uri = redirect_uri
this.scope = scope.join(' ')
this.state = state
this.code = code
this.refresh_token = undefined
this.force_verify = force_verify
// this.token = token
this.granted_scopes = undefined
this.user = undefined
}
redirect() {
const parsedOptions = toQueryParams({
client_id: this.client_id,
force_verify: this.force_verify,
redirect_uri: this.redirect_uri,
response_type: 'code',
scope: this.scope,
state: this.state,
})
return `https://id.twitch.tv/oauth2/authorize?${parsedOptions}`
}
private async getTokenFromCode() {
const parsedOptions = toQueryParams({
client_id: this.client_id,
client_secret: this.client_secret,
code: this.code,
grant_type: 'authorization_code',
redirect_uri: this.redirect_uri,
})
const url = 'https://id.twitch.tv/oauth2/token'
const response = (await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: parsedOptions,
}).then((res) => res.json<TwitchTokenResponse>()))
if ('error' in response) {
throw new HTTPException(400, { message: response.error })
}
if ('access_token' in response) {
this.token = {
token: response.access_token,
expires_in: response.expires_in,
}
}
if ('refresh_token' in response) {
this.refresh_token = {
token: response.refresh_token,
expires_in: 0,
}
}
if ('scope' in response) {
this.granted_scopes = response.scope
}
}
async getUserData() {
await this.getTokenFromCode()
const response = (await fetch('https://api.twitch.tv/helix/users', {
headers: {
authorization: `Bearer ${this.token?.token}`,
'Client-ID': this.client_id,
},
}).then((res) => res.json())) as TwitchUserResponse | TwitchErrorResponse
if ('error' in response) {
throw new HTTPException(400, { message: JSON.stringify(response) })
}
if ('message' in response) {
throw new HTTPException(400, { message: JSON.stringify(response) })
}
if ('data' in response) {
this.user = response.data[0]
}
}
}