我在我的NestJS应用程序中实现了Google和Dropbox身份验证,使用了两种不同的策略。
问题是我永远无法获得refresh_token与access_token一起。我已经尝试从我的Google / Dropbox帐户中删除已授予的应用程序,但没有成功。
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-google-oauth2';
import { AuthService } from './auth.service';
import { OauthUser } from './oauth-user.entity';
import { UserService } from '../user/user.service';
import { ConfigService } from '../config/config.service';
import { CloudProvider } from '../shared/enums/cloud-service.enum';
@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
constructor(private readonly authService: AuthService,
private readonly userService: UserService,
private readonly configService: ConfigService) {
super({
clientID: configService.get('OAUTH_GOOGLE_ID'),
clientSecret: configService.get('OAUTH_GOOGLE_SECRET'),
callbackURL: configService.get('OAUTH_GOOGLE_CALLBACK'),
passReqToCallback: true,
scope: ['email', 'profile', 'https://www.googleapis.com/auth/drive.file'],
accessType: 'offline',
prompt: 'consent',
session: false,
});
}
async validate(request: any, accessToken: string, refreshToken: string, oauthUser: OauthUser) {
console.log('accessToken', accessToken) // <- this one is good
console.log('refreshtoken', refreshToken) // <- always undefined
oauthUser.provider = CloudProvider.GOOGLE;
return this.userService.getOrCreate(oauthUser, accessToken).then(() => {
return this.authService.getJwtForUser(oauthUser).then(jwt => {
return { jwt };
});
});
}
}