Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

chore: user displayName and photoURL property updates itself when found with SSO providers

Balu Babub2acd5511c04
diff --git a/packages/hoppscotch-backend/src/auth/strategies/github.strategy.ts b/packages/hoppscotch-backend/src/auth/strategies/github.strategy.ts
index dab631439..08cc3d640 100644
--- a/packages/hoppscotch-backend/src/auth/strategies/github.strategy.ts
+++ b/packages/hoppscotch-backend/src/auth/strategies/github.strategy.ts
@@ -4,6 +4,7 @@ import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from '../auth.service';
import { UserService } from 'src/user/user.service';
import * as O from 'fp-ts/Option';
+import * as E from 'fp-ts/Either';
@Injectable()
export class GithubStrategy extends PassportStrategy(Strategy) {
@@ -33,6 +34,16 @@ export class GithubStrategy extends PassportStrategy(Strategy) {
return createdUser;
}
+ if (!user.value.displayName || !user.value.photoURL) {
+ const updatedUser = await this.usersService.updateUserDetails(
+ user.value,
+ profile,
+ );
+ if (E.isLeft(updatedUser)) {
+ throw new UnauthorizedException(updatedUser.left);
+ }
+ }
+
/**
* * 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
diff --git a/packages/hoppscotch-backend/src/auth/strategies/google.strategy.ts b/packages/hoppscotch-backend/src/auth/strategies/google.strategy.ts
index 5f81ee0ab..93e2d4ec4 100644
--- a/packages/hoppscotch-backend/src/auth/strategies/google.strategy.ts
+++ b/packages/hoppscotch-backend/src/auth/strategies/google.strategy.ts
@@ -1,9 +1,10 @@
import { Strategy, VerifyCallback } from 'passport-google-oauth20';
import { PassportStrategy } from '@nestjs/passport';
-import { Injectable } from '@nestjs/common';
+import { Injectable, UnauthorizedException } from '@nestjs/common';
import { UserService } from 'src/user/user.service';
import * as O from 'fp-ts/Option';
import { AuthService } from '../auth.service';
+import * as E from 'fp-ts/Either';
@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy) {
@@ -33,6 +34,16 @@ export class GoogleStrategy extends PassportStrategy(Strategy) {
return createdUser;
}
+ if (!user.value.displayName || !user.value.photoURL) {
+ const updatedUser = await this.usersService.updateUserDetails(
+ user.value,
+ profile,
+ );
+ if (E.isLeft(updatedUser)) {
+ throw new UnauthorizedException(updatedUser.left);
+ }
+ }
+
/**
* * 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
diff --git a/packages/hoppscotch-backend/src/auth/strategies/microsoft.strategy.ts b/packages/hoppscotch-backend/src/auth/strategies/microsoft.strategy.ts
index ecfcd4536..085643ec6 100644
--- a/packages/hoppscotch-backend/src/auth/strategies/microsoft.strategy.ts
+++ b/packages/hoppscotch-backend/src/auth/strategies/microsoft.strategy.ts
@@ -4,6 +4,7 @@ import { Injectable, UnauthorizedException } from '@nestjs/common';
import { AuthService } from '../auth.service';
import { UserService } from 'src/user/user.service';
import * as O from 'fp-ts/Option';
+import * as E from 'fp-ts/Either';
@Injectable()
export class MicrosoftStrategy extends PassportStrategy(Strategy) {
@@ -33,6 +34,16 @@ export class MicrosoftStrategy extends PassportStrategy(Strategy) {
return createdUser;
}
+ if (!user.value.displayName || !user.value.photoURL) {
+ const updatedUser = await this.usersService.updateUserDetails(
+ user.value,
+ profile,
+ );
+ if (E.isLeft(updatedUser)) {
+ throw new UnauthorizedException(updatedUser.left);
+ }
+ }
+
/**
* * 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
diff --git a/packages/hoppscotch-backend/src/user/user.service.ts b/packages/hoppscotch-backend/src/user/user.service.ts
index 92cea1849..8c57ce85c 100644
--- a/packages/hoppscotch-backend/src/user/user.service.ts
+++ b/packages/hoppscotch-backend/src/user/user.service.ts
@@ -1,7 +1,9 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from 'src/prisma/prisma.service';
import * as O from 'fp-ts/Option';
+import * as E from 'fp-ts/Either';
import { AuthUser } from 'src/types/AuthUser';
+import { USER_NOT_FOUND } from 'src/errors';
@Injectable()
export class UserService {
@@ -91,4 +93,21 @@ export class UserService {
return createdProvider;
}
+
+ async updateUserDetails(user: AuthUser, profile) {
+ try {
+ const updatedUser = await this.prisma.user.update({
+ where: {
+ uid: user.uid,
+ },
+ data: {
+ displayName: !profile.displayName ? null : profile.displayName,
+ photoURL: !profile.photos ? null : profile.photos[0].value,
+ },
+ });
+ return E.right(updatedUser);
+ } catch (error) {
+ return E.left(USER_NOT_FOUND);
+ }
+ }
}