2021-04-13 21:53:58 +02:00
|
|
|
import { Injectable } from '@nestjs/common';
|
|
|
|
import { PassportStrategy } from '@nestjs/passport';
|
|
|
|
import { Provider } from '@prisma/client';
|
|
|
|
import { Strategy } from 'passport-google-oauth20';
|
|
|
|
|
2021-04-18 19:06:54 +02:00
|
|
|
import { ConfigurationService } from '../../services/configuration.service';
|
2021-04-13 21:53:58 +02:00
|
|
|
import { AuthService } from './auth.service';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
|
2021-04-18 19:06:54 +02:00
|
|
|
public constructor(
|
|
|
|
private readonly authService: AuthService,
|
|
|
|
readonly configurationService: ConfigurationService
|
|
|
|
) {
|
2021-04-13 21:53:58 +02:00
|
|
|
super({
|
2021-04-18 19:06:54 +02:00
|
|
|
callbackURL: `${configurationService.get(
|
|
|
|
'ROOT_URL'
|
|
|
|
)}/api/auth/google/callback`,
|
|
|
|
clientID: configurationService.get('GOOGLE_CLIENT_ID'),
|
|
|
|
clientSecret: configurationService.get('GOOGLE_SECRET'),
|
2021-04-13 21:53:58 +02:00
|
|
|
passReqToCallback: true,
|
|
|
|
scope: ['email', 'profile']
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public async validate(
|
|
|
|
request: any,
|
|
|
|
token: string,
|
|
|
|
refreshToken: string,
|
|
|
|
profile,
|
|
|
|
done: Function,
|
|
|
|
done2: Function
|
|
|
|
) {
|
|
|
|
try {
|
|
|
|
const jwt: string = await this.authService.validateOAuthLogin({
|
|
|
|
provider: Provider.GOOGLE,
|
|
|
|
thirdPartyId: profile.id
|
|
|
|
});
|
|
|
|
const user = {
|
|
|
|
jwt
|
|
|
|
};
|
|
|
|
|
|
|
|
done(null, user);
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
done(err, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|