From 227870ac781c07c23854218b2d0cda2349bec015 Mon Sep 17 00:00:00 2001 From: Amruth Pillai Date: Mon, 13 Jan 2025 16:22:29 +0100 Subject: [PATCH] fix issues suggested by coderabbit --- apps/server/src/auth/auth.service.ts | 4 ++++ apps/server/src/auth/strategy/github.strategy.ts | 6 +++--- apps/server/src/auth/strategy/google.strategy.ts | 6 +++--- apps/server/src/auth/strategy/openid.strategy.ts | 8 ++++---- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/apps/server/src/auth/auth.service.ts b/apps/server/src/auth/auth.service.ts index 487f89b7f..122df9a39 100644 --- a/apps/server/src/auth/auth.service.ts +++ b/apps/server/src/auth/auth.service.ts @@ -200,6 +200,10 @@ export class AuthService { } if ( + this.configService.get("OPENID_AUTHORIZATION_URL") && + this.configService.get("OPENID_ISSUER") && + this.configService.get("OPENID_TOKEN_URL") && + this.configService.get("OPENID_USER_INFO_URL") && this.configService.get("OPENID_CLIENT_ID") && this.configService.get("OPENID_CLIENT_SECRET") && this.configService.get("OPENID_CALLBACK_URL") diff --git a/apps/server/src/auth/strategy/github.strategy.ts b/apps/server/src/auth/strategy/github.strategy.ts index 7425c6de5..f258a3726 100644 --- a/apps/server/src/auth/strategy/github.strategy.ts +++ b/apps/server/src/auth/strategy/github.strategy.ts @@ -33,11 +33,11 @@ export class GitHubStrategy extends PassportStrategy(Strategy, "github") { if (!email) throw new BadRequestException(ErrorMessage.InvalidCredentials); try { - const user = + user = (await this.userService.findOneByIdentifier(email)) ?? - (username && (await this.userService.findOneByIdentifier(username))); + (username ? await this.userService.findOneByIdentifier(username) : null); - if (!user) throw new Error(ErrorMessage.InvalidCredentials); + if (!user) throw new BadRequestException(ErrorMessage.InvalidCredentials); done(null, user); } catch { diff --git a/apps/server/src/auth/strategy/google.strategy.ts b/apps/server/src/auth/strategy/google.strategy.ts index ff446d4b2..9c06c6541 100644 --- a/apps/server/src/auth/strategy/google.strategy.ts +++ b/apps/server/src/auth/strategy/google.strategy.ts @@ -33,11 +33,11 @@ export class GoogleStrategy extends PassportStrategy(Strategy, "google") { if (!email) throw new BadRequestException(ErrorMessage.InvalidCredentials); try { - const user = + user = (await this.userService.findOneByIdentifier(email)) ?? - (username && (await this.userService.findOneByIdentifier(username))); + (username ? await this.userService.findOneByIdentifier(username) : null); - if (!user) throw new Error(ErrorMessage.InvalidCredentials); + if (!user) throw new BadRequestException(ErrorMessage.InvalidCredentials); done(null, user); } catch { diff --git a/apps/server/src/auth/strategy/openid.strategy.ts b/apps/server/src/auth/strategy/openid.strategy.ts index 97cf958e0..ec9cb7a2d 100644 --- a/apps/server/src/auth/strategy/openid.strategy.ts +++ b/apps/server/src/auth/strategy/openid.strategy.ts @@ -37,7 +37,7 @@ export class OpenIDStrategy extends PassportStrategy(Strategy, "openid") { ) { const { displayName, emails, photos, username } = profile; - const email = emails?.[0].value ?? `${username}@github.com`; + const email = emails?.[0].value ?? `${username}@openid.com`; const picture = photos?.[0].value; let user: User | null = null; @@ -45,11 +45,11 @@ export class OpenIDStrategy extends PassportStrategy(Strategy, "openid") { if (!email) throw new BadRequestException(ErrorMessage.InvalidCredentials); try { - const user = + user = (await this.userService.findOneByIdentifier(email)) ?? - (username && (await this.userService.findOneByIdentifier(username))); + (username ? await this.userService.findOneByIdentifier(username) : null); - if (!user) throw new Error(ErrorMessage.InvalidCredentials); + if (!user) throw new BadRequestException(ErrorMessage.InvalidCredentials); done(null, user); } catch {