7 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
sugoidogo 83f3217565 keep refresh token
docker.yml / build (push) Successful in 46s
2026-08-02 03:53:56 -07:00
sugoidogo a704849061 uphold userid promise
docker.yml / build (push) Successful in 48s
2026-08-02 03:49:31 -07:00
2 changed files with 18 additions and 11 deletions
-5
View File
@@ -1,5 +0,0 @@
{
"files.associations": {
"wrangler.json": "jsonc"
}
}
+18 -6
View File
@@ -2,7 +2,18 @@ import type { AccessTokenMaybeWithUserId, AccessTokenWithUserId, AuthProvider }
import type { UserIdResolvable } from "@twurple/api"
import * as TwitchAuth from "./TwitchAuth";
function getTwurpleProxy(token: TwitchAuth.TwitchToken): AccessTokenWithUserId {
async function ensureUserId(token: TwitchAuth.TwitchToken | null): Promise<TwitchAuth.TwitchToken | null> {
if (!token) return null
if (!token.user_id) {
const refresh_token = token.refresh_token
token = await TwitchAuth.validateToken(token.access_token)
token.refresh_token = refresh_token
}
return token
}
async function getTwurpleProxy(token: TwitchAuth.TwitchToken): Promise<AccessTokenWithUserId> {
token=await ensureUserId(token)
return new Proxy(token, {
get(target, name, receiver) {
return target[name.toString().replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`) as keyof TwitchAuth.TwitchToken]
@@ -11,12 +22,12 @@ function getTwurpleProxy(token: TwitchAuth.TwitchToken): AccessTokenWithUserId {
}
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;
constructor(clientId:string,proxyOrigin?:string) {
this.clientId = clientId;
if (proxyOrigin) TwitchAuth.setProxyOrigin(proxyOrigin);
this.#token=TwitchAuth.getUserTokenPassive(clientId).catch()
this.#token = TwitchAuth.getUserTokenPassive(clientId).then(ensureUserId)
}
#authorize(scopes: string[]) {
const promise = TwitchAuth.requestAuthCode(this.clientId, ...scopes)
@@ -29,7 +40,7 @@ export class SugoiAuthProvider implements AuthProvider {
*
* 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
if(!token) return this.#authorize(scopes)
for (const scope of scopes) {
@@ -37,6 +48,7 @@ export class SugoiAuthProvider implements AuthProvider {
return this.#authorize(scopes)
}
}
return getTwurpleProxy(token)
}
getCurrentScopesForUser(user: UserIdResolvable):string[] {
if (!this.#token || this.#token instanceof Promise) throw new Error("unauthorized")
@@ -56,8 +68,8 @@ export class SugoiAuthProvider implements AuthProvider {
const token=await this.#token
if (!token) throw new Error("unauthorized")
if (!token.refresh_token) throw new Error("missing refresh token")
this.#token = await TwitchAuth.refreshToken(this.clientId, token.refresh_token)
return getTwurpleProxy(this.#token)
this.#token = await TwitchAuth.refreshToken(this.clientId, token.refresh_token).then(ensureUserId)
return getTwurpleProxy(this.#token!)
}
}