From 82dee95cd03b85acc0dce2658cf4bac8683b4faa Mon Sep 17 00:00:00 2001 From: Balu Babu Date: Thu, 19 Jan 2023 04:55:51 +0530 Subject: [PATCH] chore: added types to createProviderAccount ,ethod in auth.service file --- .../hoppscotch-backend/src/auth/auth.service.ts | 5 ++++- .../src/auth/strategies/github.strategy.ts | 6 ++++-- .../src/auth/strategies/google.strategy.ts | 5 ++++- .../src/auth/strategies/jwt.strategy.ts | 17 ----------------- .../src/auth/strategies/microsoft.strategy.ts | 5 ++++- .../hoppscotch-backend/src/user/user.service.ts | 9 +++++++-- 6 files changed, 23 insertions(+), 24 deletions(-) diff --git a/packages/hoppscotch-backend/src/auth/auth.service.ts b/packages/hoppscotch-backend/src/auth/auth.service.ts index d95a0fc1f..f2254b104 100644 --- a/packages/hoppscotch-backend/src/auth/auth.service.ts +++ b/packages/hoppscotch-backend/src/auth/auth.service.ts @@ -224,7 +224,10 @@ export class AuthService { statusCode: HttpStatus.NOT_FOUND, }); - // Check to see if entry for magic-link is present in the Account table for this user + /** + * * Check to see if entry for Magic-Link is present in the Account table for user + * * If user was created with another provider findUserByEmail may return true + */ const profile = { provider: 'email', id: user.value.email, diff --git a/packages/hoppscotch-backend/src/auth/strategies/github.strategy.ts b/packages/hoppscotch-backend/src/auth/strategies/github.strategy.ts index d321548fa..dab631439 100644 --- a/packages/hoppscotch-backend/src/auth/strategies/github.strategy.ts +++ b/packages/hoppscotch-backend/src/auth/strategies/github.strategy.ts @@ -20,7 +20,6 @@ export class GithubStrategy extends PassportStrategy(Strategy) { } async validate(accessToken, refreshToken, profile, done) { - console.dir(profile); const user = await this.usersService.findUserByEmail( profile.emails[0].value, ); @@ -34,7 +33,10 @@ export class GithubStrategy extends PassportStrategy(Strategy) { return createdUser; } - // Check to see if entry for github is present in the Account table for this user + /** + * * Check to see if entry for Github is present in the Account table for user + * * If user was created with another provider findUserByEmail may return true + */ const providerAccountExists = await this.authService.checkIfProviderAccountExists(user.value, profile); diff --git a/packages/hoppscotch-backend/src/auth/strategies/google.strategy.ts b/packages/hoppscotch-backend/src/auth/strategies/google.strategy.ts index ad1f48a47..5f81ee0ab 100644 --- a/packages/hoppscotch-backend/src/auth/strategies/google.strategy.ts +++ b/packages/hoppscotch-backend/src/auth/strategies/google.strategy.ts @@ -33,7 +33,10 @@ export class GoogleStrategy extends PassportStrategy(Strategy) { return createdUser; } - // Check to see if entry for google is present in the Account table for this user + /** + * * Check to see if entry for Google is present in the Account table for user + * * If user was created with another provider findUserByEmail may return true + */ const providerAccountExists = await this.authService.checkIfProviderAccountExists(user.value, profile); diff --git a/packages/hoppscotch-backend/src/auth/strategies/jwt.strategy.ts b/packages/hoppscotch-backend/src/auth/strategies/jwt.strategy.ts index 735027cd0..6822b04f1 100644 --- a/packages/hoppscotch-backend/src/auth/strategies/jwt.strategy.ts +++ b/packages/hoppscotch-backend/src/auth/strategies/jwt.strategy.ts @@ -40,27 +40,10 @@ export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') { if (!payload) throw new ForbiddenException(INVALID_ACCESS_TOKEN); const user = await this.usersService.findUserById(payload.sub); - if (O.isNone(user)) { throw new UnauthorizedException(USER_NOT_FOUND); } - const profile = { - provider: 'email', - id: user.value.email, - }; - - const providerAccountExists = - await this.authService.checkIfProviderAccountExists(user.value, profile); - - if (!providerAccountExists) - await this.usersService.createProviderAccount( - user.value, - null, - null, - profile, - ); - return user.value; } } diff --git a/packages/hoppscotch-backend/src/auth/strategies/microsoft.strategy.ts b/packages/hoppscotch-backend/src/auth/strategies/microsoft.strategy.ts index 7bf0a6205..ecfcd4536 100644 --- a/packages/hoppscotch-backend/src/auth/strategies/microsoft.strategy.ts +++ b/packages/hoppscotch-backend/src/auth/strategies/microsoft.strategy.ts @@ -33,7 +33,10 @@ export class MicrosoftStrategy extends PassportStrategy(Strategy) { return createdUser; } - // Check to see if entry for microsoft is present in the Account table for this user + /** + * * Check to see if entry for Microsoft is present in the Account table for user + * * If user was created with another provider findUserByEmail may return true + */ const providerAccountExists = await this.authService.checkIfProviderAccountExists(user.value, profile); diff --git a/packages/hoppscotch-backend/src/user/user.service.ts b/packages/hoppscotch-backend/src/user/user.service.ts index 5662985ea..1f42d79df 100644 --- a/packages/hoppscotch-backend/src/user/user.service.ts +++ b/packages/hoppscotch-backend/src/user/user.service.ts @@ -1,7 +1,7 @@ import { Injectable } from '@nestjs/common'; import { PrismaService } from 'src/prisma/prisma.service'; import * as O from 'fp-ts/Option'; -import { AuthUser, SSOProviderProfile } from 'src/types/AuthUser'; +import { AuthUser } from 'src/types/AuthUser'; @Injectable() export class UserService { @@ -69,7 +69,12 @@ export class UserService { return createdUser; } - async createProviderAccount(user, accessToken, refreshToken, profile) { + async createProviderAccount( + user: AuthUser, + accessToken: string, + refreshToken: string, + profile, + ) { const createdProvider = await this.prisma.account.create({ data: { provider: profile.provider,