Skip to content

Commit

Permalink
feat: improved scope handling
Browse files Browse the repository at this point in the history
  • Loading branch information
veryCrunchy committed May 3, 2024
1 parent 45c343a commit 7e66939
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class Auth implements AuthProvider {
accessToken: result.access_token,
refreshToken: result.refresh_token,
accessTokenExpiresAt: createDate(new TimeSpan(result.expires_in, "s")),
scope: String(result.scope).replace(",", " "),
};
return tokens;
}
Expand All @@ -84,6 +85,7 @@ export class Auth implements AuthProvider {
accessToken: result.access_token,
refreshToken: result.refresh_token,
accessTokenExpiresAt: createDate(new TimeSpan(result.expires_in, "s")),
scope: String(result.scope).replace(",", " "),
};
return tokens;
}
Expand All @@ -98,12 +100,14 @@ interface TokenResponseBody {
access_token: string;
expires_in: number;
refresh_token: string;
scope: string;
}

export interface Tokens {
accessToken: string;
refreshToken: string;
accessTokenExpiresAt: Date;
scope: string;
}

export interface DiscordUserResponse {
Expand Down
12 changes: 2 additions & 10 deletions src/routes/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,15 @@ router.get("/login/callback", async (req: Request, res: Response) => {
},
}
);
const twitchScopeResponse = await fetch(
"https://id.twitch.tv/oauth2/validate",
{
headers: {
Authorization: `Bearer ${tokens.accessToken}`,
"Client-Id": env.TWITCH_CLIENT_ID,
},
}
);

const twitchUser: TwitchUserResponse = (await twitchUserResponse.json())
.data[0];
user.id = twitchUser.id;
user.email = twitchUser.email;
user.username = twitchUser.login;
user.displayName = twitchUser.display_name;
user.avatar = twitchUser.profile_image_url;
user.scopes = (await twitchScopeResponse.json()).scopes as string[];
user.scopes = tokens.scope.split(" ");
}
if (platform === "discord") {
tokens = await discordAuth.validateAuthorizationCode(code);
Expand Down

0 comments on commit 7e66939

Please sign in to comment.