Skip to content

Commit

Permalink
feat: add handle googlecallback service
Browse files Browse the repository at this point in the history
  • Loading branch information
LhonRafaat committed Oct 11, 2024
1 parent 66b975f commit 3a9338b
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/modules/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -48,6 +49,27 @@ export class AuthService {
return tokens;
}

async handleGoogleCallback(req: IRequest): Promise<TAuthResponse> {
let user = await this.usersService.findByEmail(req.user.email);

if (!user) {
user = await this.usersService.createWithProvider(
req.user as Omit<RegisterPayload, 'password' | 'isAdmin'> & {
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, {
Expand Down

0 comments on commit 3a9338b

Please sign in to comment.