From 587556b55b0f4e5ce3e7b7e4b8e846020c97ab1f Mon Sep 17 00:00:00 2001 From: Kalle <38327916+Sendouc@users.noreply.github.com> Date: Sun, 24 Sep 2023 23:12:01 +0300 Subject: [PATCH] Restructure auth code + debug logs --- app/modules/auth/DiscordStrategy.server.ts | 29 ++++++++++++---------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/app/modules/auth/DiscordStrategy.server.ts b/app/modules/auth/DiscordStrategy.server.ts index c4a8d1d07..19341856d 100644 --- a/app/modules/auth/DiscordStrategy.server.ts +++ b/app/modules/auth/DiscordStrategy.server.ts @@ -53,21 +53,9 @@ export class DiscordStrategy extends OAuth2Strategy< callbackURL: new URL("/auth/callback", envVars.BASE_URL).toString(), }, async ({ accessToken }) => { - const authHeader: [string, string] = [ - "Authorization", - `Bearer ${accessToken}`, - ]; - const discordResponses = this.authGatewayEnabled() ? await this.fetchProfileViaGateway(accessToken) - : await Promise.all([ - fetch("https://discord.com/api/users/@me", { - headers: [authHeader], - }).then(this.jsonIfOk), - fetch("https://discord.com/api/users/@me/connections", { - headers: [authHeader], - }).then(this.jsonIfOk), - ]); + : await this.fetchProfileViaDiscordApi(accessToken); const [user, connections] = discordUserDetailsSchema.parse(discordResponses); @@ -94,7 +82,22 @@ export class DiscordStrategy extends OAuth2Strategy< ); } + private async fetchProfileViaDiscordApi(token: string) { + console.log("authenticating via discord..."); + const authHeader: [string, string] = ["Authorization", `Bearer ${token}`]; + + return Promise.all([ + fetch("https://discord.com/api/users/@me", { + headers: [authHeader], + }).then(this.jsonIfOk), + fetch("https://discord.com/api/users/@me/connections", { + headers: [authHeader], + }).then(this.jsonIfOk), + ]); + } + private async fetchProfileViaGateway(token: string) { + console.log("authenticating via gateway..."); const url = `${process.env["AUTH_GATEWAY_URL"]}?token=${token}`; const options: RequestInit = {