diff --git a/src/modules/auth/auth.service.ts b/src/modules/auth/auth.service.ts index 37631d1..73ca84e 100644 --- a/src/modules/auth/auth.service.ts +++ b/src/modules/auth/auth.service.ts @@ -7,6 +7,7 @@ import { JwtService } from '@nestjs/jwt'; import { ConfigService } from '@nestjs/config'; import { EnvConfig } from '../../config.type'; import { TAuthResponse } from './types/auth.response'; +import { IRequest } from '../../common/helper/common-types'; @Injectable() export class AuthService { @@ -48,6 +49,27 @@ export class AuthService { return tokens; } + async handleGoogleCallback(req: IRequest): Promise { + let user = await this.usersService.findByEmail(req.user.email); + + if (!user) { + user = await this.usersService.createWithProvider( + req.user as Omit & { + oauthProvider: string; + oauthProviderId: string; + }, + ); + } + + const tokens = await this.getTokens({ + _id: user._id, + fullName: user.fullName, + }); + + await this.updateRefreshToken(user._id, tokens.refreshToken); + return tokens; + } + async getTokens(payload: { _id: string; fullName: string }) { const [accessToken, refreshToken] = await Promise.all([ this.jwtService.signAsync(payload, {