Extending `PassportStrategy` does not take provider specific OAuth2 options in account
See original GitHub issueI’m submitting a…
[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
Current behavior
Extending PassportStrategy
does not work as expected. In case of extending PassportStrategy(Strategy, 'google')
additional OAuth2 options, respectively provider specific options like e.g. approval_prompt
passed to Superconstructor are NOT applied. So it is not possible to obtain a REFRESH_TOKEN.
Expected behavior
Additional, provider specific OAuth2 options can be passed through the Superconstructor and become effective.
Minimal reproduction of the problem with instructions
My Google OAuth2 strategy implementation:
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { Strategy } from 'passport-google-oauth20';
import { AuthService, Provider } from './auth.service';
import { ConfigService } from '../config/config.service';
import { User } from '../api/user/user.interface';
@Injectable()
export class GoogleStrategy extends PassportStrategy(Strategy, 'google') {
constructor(configService: ConfigService, private readonly authService: AuthService) {
super({
clientID: configService.get('OAUTH_CLIENT_ID'),
clientSecret: configService.get('OAUTH_CLIENT_SECRET'),
callbackURL: `${configService.baseUrl}/auth/google/callback`,
passReqToCallback: true,
scope: ['email', 'profile'],
// NOT WORKING
approval_prompt: 'force',
access_type: 'offline',
});
}
async validate(request: any, accessToken: string, refreshToken: string, profile: any, done: (err: any, result: any) => void) {
// ...
}
// WORKAROUND: pass options to superclass auth call by overriding superclass method
authorizationParams(options: any): any {
return Object.assign(options, {
approval_prompt: 'force',
access_type: 'offline',
});
}
}
Environment
@nestjs/passport: 6.0.0
For Tooling issues:
- Node version: 10.11.0
- Platform: Mac
Issue Analytics
- State:
- Created 4 years ago
- Reactions:11
- Comments:30 (12 by maintainers)
Top Results From Across the Web
Authentication | NestJS - A progressive Node.js framework
A set of options that are specific to that strategy. ... With @nestjs/passport , you configure a Passport strategy by extending the PassportStrategy...
Read more >How to trigger callback in passport-oauth2 - node.js
This is an example of a find and update function of mine. passport.use('oauth2', new OAuth2Strategy({ authorizationURL: 'http://localhost ...
Read more >Nest.JS Tutorial Part #2 - Setting up Passport, OAuth2 ...
In this second episode of our NestJS application, we will setup OAuth2 with Passport, Sessions, Session Store, TypeORM, and MySQL.
Read more >NestJS & Google OAuth2 with Passport - YouTube
Code: https://github.com/stuyy/google-nestjs-oauth2Support the Channel:Become a Member: https://www.youtube.com/ansonthedeveloper/joinBecome ...
Read more >Complete Guide to Multi-Provider OAuth 2 Authorization in ...
Extending the code to allow adding multiple Google accounts. ... OAuth 2 does not provide a user authentication mechanism, by design.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@iveselin just try with:
The problem is, that non-standard params passed through constructor
super()
call are ignored, which normally should work (https://github.com/jaredhanson/passport-google-oauth2/blob/master/lib/strategy.js#L159). Just useauthorizationParams
as in the example above.https://github.com/nestjs/passport/blob/master/lib/auth.guard.ts#L82
We pass
AuthModuleOptions
to theauthenticate()
function, so you should be able to put them here: