All files google-auth.service.ts

90% Statements 9/10
75% Branches 3/4
100% Functions 2/2
88.88% Lines 8/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43                1x         2x     2x             5x   5x         4x   4x       4x            
import { Inject, Injectable } from '@nestjs/common'
import { OAuth2Client } from 'google-auth-library';
import { THIRD_PARTY_OPTIONS_PROVIDER } from './constants';
import { IThirdPartyAuthPluginOptions } from './interfaces';
type WithRequire<T, K extends keyof T> = T & {
    [P in K]-?: T[P]
}
@Injectable()
export class GoogleAuthService {
    private googleClient: OAuth2Client;
 
    constructor(
        @Inject(THIRD_PARTY_OPTIONS_PROVIDER)
        private options: Required<Pick<IThirdPartyAuthPluginOptions, "google">>
    ) {
 
        this.googleClient = new OAuth2Client({
            clientId:  options.google.clientId,
            clientSecret: options.google.clientSecret
        })
    }
 
    async verify(code: string) {
        const clientId = this.options.google.clientId
 
        const ticket = await this.googleClient.verifyIdToken({
            idToken: code,
            audience: clientId
        })
        
        const payload = ticket.getPayload()
 
        Iif (!payload || !payload.email) {
            throw new Error('Cannot get neither ticket payload or email');
        }
        
        return payload as WithRequire<typeof payload, "email">
    }
}
interface IUserInfo {
    given_name: string
    family_name: string
}