google oauth working

This commit is contained in:
Gabriel Rosa
2022-06-24 13:03:16 -03:00
parent 07f123e528
commit 498ea61c06
5 changed files with 64 additions and 16 deletions
+1 -1
View File
@@ -78,7 +78,7 @@ export class OauthController {
if (!authInfo) throw new BadRequestException('Oauth tokens not found');
const { accessToken: access_token, refreshToken: refresh_token } = authInfo;
const { customer_id } = request.query;
const { state: customer_id } = request.query;
const response = await this.inputService.create({
info: { customer_id, customer: 'customer', user_id: 'empty' },
@@ -1,39 +1,47 @@
import {
Profile,
Strategy,
StrategyOptionsWithRequest,
VerifyCallback,
} from 'passport-google-oauth20';
import { Profile, Strategy, StrategyOptions } from 'passport-google-oauth20';
import { PassportStrategy } from '@nestjs/passport';
import { Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable, Req } from '@nestjs/common';
import { OauthSecrets } from 'src/utils/OauthSecrets';
@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy) {
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
constructor(
@Inject('OAUTH_SECRETS')
private readonly oauthSecrets: OauthSecrets,
) {
const options: StrategyOptionsWithRequest = {
const options: StrategyOptions = {
clientID: oauthSecrets.google.client_id,
clientSecret: oauthSecrets.google.client_secret,
scope: 'drive.metadata.readonly spreadsheets.readonly',
scope: [
'https://www.googleapis.com/auth/drive.metadata.readonly',
'https://www.googleapis.com/auth/spreadsheets.readonly',
'https://www.googleapis.com/auth/userinfo.email',
],
callbackURL: oauthSecrets.google.redirect_uri,
passReqToCallback: true,
};
const verify = (
accessToken: string,
refreshToken: string,
profile: Profile,
done: VerifyCallback,
) => done(null, profile, { accessToken, refreshToken });
done,
) => {
console.log('verify');
return done(null, profile, { accessToken, refreshToken });
};
super(options, verify);
}
authenticate(req, options) {
authenticate(@Req() req, options) {
const { customer_id } = req.query;
options.state = customer_id;
options.accessType = 'offline';
options.prompt = 'consent';
super.authenticate(req, options);
}
async validate(...args) {
console.log('validate');
console.log(args);
}
}