Skip to content

Commit

Permalink
feat: added google auth support for mobile devices
Browse files Browse the repository at this point in the history
  • Loading branch information
Homoakin619 committed Aug 14, 2024
1 parent f598711 commit baf0630
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/modules/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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()
Expand Down
12 changes: 10 additions & 2 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' });

Expand Down

0 comments on commit baf0630

Please sign in to comment.