5 Commits
Author SHA1 Message Date
sugoidogo 76cd0649b1 return token instead of void
docker.yml / build (push) Successful in 1m13s
2026-08-05 22:26:49 -07:00
sugoidogo e3c12df6fb remove editor config from repo 2026-08-05 22:25:00 -07:00
sugoidogo a27f8804e4 fix type error part 2
docker.yml / build (push) Successful in 46s
2026-08-02 04:18:24 -07:00
sugoidogo 2788275d25 fix type error
docker.yml / build (push) Failing after 39s
2026-08-02 04:15:36 -07:00
sugoidogo 2265fd39e0 always ensure userId is present on tokens
docker.yml / build (push) Failing after 35s
2026-08-02 04:11:11 -07:00
2 changed files with 15 additions and 13 deletions
-5
View File
@@ -1,5 +0,0 @@
{
"files.associations": {
"wrangler.json": "jsonc"
}
}
+15 -8
View File
@@ -2,12 +2,18 @@ import type { AccessTokenMaybeWithUserId, AccessTokenWithUserId, AuthProvider }
import type { UserIdResolvable } from "@twurple/api" import type { UserIdResolvable } from "@twurple/api"
import * as TwitchAuth from "./TwitchAuth"; import * as TwitchAuth from "./TwitchAuth";
async function getTwurpleProxy(token: TwitchAuth.TwitchToken): Promise<AccessTokenWithUserId> { async function ensureUserId(token: TwitchAuth.TwitchToken | null): Promise<TwitchAuth.TwitchToken | null> {
if (!token) return null
if (!token.user_id) { if (!token.user_id) {
const refresh_token=token.refresh_token const refresh_token = token.refresh_token
token = await TwitchAuth.validateToken(token.access_token) token = await TwitchAuth.validateToken(token.access_token)
token.refresh_token=refresh_token token.refresh_token = refresh_token
} }
return token
}
async function getTwurpleProxy(token: TwitchAuth.TwitchToken): Promise<AccessTokenWithUserId> {
token=await ensureUserId(token)
return new Proxy(token, { return new Proxy(token, {
get(target, name, receiver) { get(target, name, receiver) {
return target[name.toString().replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`) as keyof TwitchAuth.TwitchToken] return target[name.toString().replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`) as keyof TwitchAuth.TwitchToken]
@@ -16,12 +22,12 @@ async function getTwurpleProxy(token: TwitchAuth.TwitchToken): Promise<AccessTok
} }
export class SugoiAuthProvider implements AuthProvider { export class SugoiAuthProvider implements AuthProvider {
#token: TwitchAuth.TwitchToken | Promise<TwitchAuth.TwitchToken | null> | null = null; #token: TwitchAuth.TwitchToken | Promise<TwitchAuth.TwitchToken> | Promise<TwitchAuth.TwitchToken | null> | null = null;
clientId: string; clientId: string;
constructor(clientId:string,proxyOrigin?:string) { constructor(clientId:string,proxyOrigin?:string) {
this.clientId = clientId; this.clientId = clientId;
if (proxyOrigin) TwitchAuth.setProxyOrigin(proxyOrigin); if (proxyOrigin) TwitchAuth.setProxyOrigin(proxyOrigin);
this.#token=TwitchAuth.getUserTokenPassive(clientId).catch() this.#token = TwitchAuth.getUserTokenPassive(clientId).then(ensureUserId)
} }
#authorize(scopes: string[]) { #authorize(scopes: string[]) {
const promise = TwitchAuth.requestAuthCode(this.clientId, ...scopes) const promise = TwitchAuth.requestAuthCode(this.clientId, ...scopes)
@@ -34,7 +40,7 @@ export class SugoiAuthProvider implements AuthProvider {
* *
* https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#get-the-user-to-authorize-your-app * https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#get-the-user-to-authorize-your-app
*/ */
async getAuthorization(...scopes: string[]): Promise<void> { async getAuthorization(...scopes: string[]): Promise<AccessTokenWithUserId> {
const token = await this.#token const token = await this.#token
if(!token) return this.#authorize(scopes) if(!token) return this.#authorize(scopes)
for (const scope of scopes) { for (const scope of scopes) {
@@ -42,6 +48,7 @@ export class SugoiAuthProvider implements AuthProvider {
return this.#authorize(scopes) return this.#authorize(scopes)
} }
} }
return getTwurpleProxy(token)
} }
getCurrentScopesForUser(user: UserIdResolvable):string[] { getCurrentScopesForUser(user: UserIdResolvable):string[] {
if (!this.#token || this.#token instanceof Promise) throw new Error("unauthorized") if (!this.#token || this.#token instanceof Promise) throw new Error("unauthorized")
@@ -61,8 +68,8 @@ export class SugoiAuthProvider implements AuthProvider {
const token=await this.#token const token=await this.#token
if (!token) throw new Error("unauthorized") if (!token) throw new Error("unauthorized")
if (!token.refresh_token) throw new Error("missing refresh token") if (!token.refresh_token) throw new Error("missing refresh token")
this.#token = await TwitchAuth.refreshToken(this.clientId, token.refresh_token) this.#token = await TwitchAuth.refreshToken(this.clientId, token.refresh_token).then(ensureUserId)
return getTwurpleProxy(this.#token) return getTwurpleProxy(this.#token!)
} }
} }