From baf06303cae255a2ebf1e66cdb73e9fa457ca385 Mon Sep 17 00:00:00 2001 From: Ismail Akintunde Date: Wed, 14 Aug 2024 16:50:45 +0100 Subject: [PATCH] feat: added google auth support for mobile devices --- src/modules/auth/auth.controller.ts | 19 ++++++++++++++++--- src/modules/auth/auth.service.ts | 12 ++++++++++-- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index e564f4276..06f60a2f1 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -8,7 +8,20 @@ import { ApiUnauthorizedResponse, } from '@nestjs/swagger'; import * as SYS_MSG from '../../helpers/SystemMessages'; -import { Body, Controller, HttpCode, HttpStatus, Post, Req, Request, Res, UseGuards, Get, Patch } from '@nestjs/common'; +import { + Body, + Controller, + HttpCode, + HttpStatus, + Post, + Req, + Request, + Res, + UseGuards, + Get, + Patch, + Query, +} from '@nestjs/common'; import { CreateUserDTO } from './dto/create-user.dto'; import { skipAuth } from '../../helpers/skipAuth'; import AuthenticationService from './auth.service'; @@ -106,8 +119,8 @@ export default class RegistrationController { @ApiResponse({ status: 200, description: 'Verify Payload sent from google', type: AuthResponseDto }) @ApiBadRequestResponse({ description: 'Invalid Google token' }) @HttpCode(200) - async googleAuth(@Body() body: GoogleAuthPayload) { - return this.authService.googleAuth(body); + async googleAuth(@Body() body: GoogleAuthPayload, @Query('mobile') isMobile: string) { + return this.authService.googleAuth({ googleAuthPayload: body, isMobile }); } @skipAuth() diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 172b483c3..63e474ce3 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -283,9 +283,17 @@ export default class AuthenticationService { }; } - async googleAuth(googleAuthPayload: GoogleAuthPayload) { + async googleAuth({ googleAuthPayload, isMobile }: { googleAuthPayload: GoogleAuthPayload; isMobile: string }) { const idToken = googleAuthPayload.id_token; - const verifyTokenResponse: GoogleVerificationPayloadInterface = await this.googleAuthService.verifyToken(idToken); + let verifyTokenResponse: GoogleVerificationPayloadInterface; + + if (isMobile === 'true') { + const request = await fetch(`https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=${idToken}`); + verifyTokenResponse = await request.json(); + } else { + verifyTokenResponse = await this.googleAuthService.verifyToken(idToken); + } + const userEmail = verifyTokenResponse.email; const userExists = await this.userService.getUserRecord({ identifier: userEmail, identifierType: 'email' });