Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

1.4 KB
Raw Blame
import { ConfigurationService } from '@ghostfolio/api/services/configuration/configuration.service';

import { Injectable, Logger } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Provider } from '@prisma/client';
import { DoneCallback } from 'passport';
import { Profile, Strategy } from 'passport-google-oauth20';

import { AuthService } from './auth.service';

@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
  private readonly logger = new Logger(GoogleStrategy.name);

  public constructor(
    private readonly authService: AuthService,
    configurationService: ConfigurationService
  ) {
    super({
      callbackURL: `${configurationService.get(
        'ROOT_URL'
      )}/api/auth/google/callback`,
      clientID: configurationService.get('GOOGLE_CLIENT_ID'),
      clientSecret: configurationService.get('GOOGLE_SECRET'),
      passReqToCallback: true,
      scope: ['profile']
    });
  }

  public async validate(
    _request: any,
    _token: string,
    _refreshToken: string,
    profile: Profile,
    done: DoneCallback
  ) {
    try {
      const jwt = await this.authService.validateOAuthLogin({
        provider: Provider.GOOGLE,
        thirdPartyId: profile.id
      });

      done(null, { jwt });
    } catch (error) {
      this.logger.error(error);
      done(error, false);
    }
  }
}