import { Injectable } from '@nestjs/common' ; import { PassportStrategy } from '@nestjs/passport' ; import * as githubPassport from 'passport-github' ; import { Metadata , StateStoreStoreCallback , StateStoreVerifyCallback } from 'passport-oauth2' ; import { AuthProviderEnum } from '@notifire/shared' ; import { AuthService } from '../auth.service' ; @ Injectable ( ) export class GithubStrategy extends PassportStrategy ( githubPassport . Strategy , 'github' ) { constructor ( private authService ) { super ( { clientID : process . env . GITHUB_OAUTH_CLIENT_ID , clientSecret : process . env . GITHUB_OAUTH_CLIENT_SECRET , callbackURL : process . env . GITHUB_OAUTH_REDIRECT , passReqToCallback : true , store : { verify ( req , state , meta , callback ) { callback ( null , true , req . query . distinctId ) ; } , store ( req , meta , callback ) { callback ( null , req . query . distinctId ) ; } , } , } ) ; } async validate ( req , accessToken , refreshToken , profile , done ) { try { const response = await this . authService . authenticate ( AuthProviderEnum . GITHUB , accessToken , refreshToken , profile . _json , req . query . state ) ; done ( null , { token : response . token , newUser : response . newUser , } ) ; } catch ( err ) { done ( err , false ) ; } } }