diff --git a/plugins/auth-backend/src/providers/config.ts b/plugins/auth-backend/src/providers/config.ts index 92d50ee208..b8df8b7e99 100644 --- a/plugins/auth-backend/src/providers/config.ts +++ b/plugins/auth-backend/src/providers/config.ts @@ -5,7 +5,6 @@ export const providers = [ clientID: process.env.GOOGLE_CLIENT_ID!, clientSecret: process.env.GOOGLE_CLIENT_SECRET!, callbackURL: 'http://localhost:7000/auth/google/handler/frame', - passReqToCallback: true, }, }, ]; diff --git a/plugins/auth-backend/src/providers/google/provider.ts b/plugins/auth-backend/src/providers/google/provider.ts index ddbb42c98b..0754e0abc0 100644 --- a/plugins/auth-backend/src/providers/google/provider.ts +++ b/plugins/auth-backend/src/providers/google/provider.ts @@ -6,35 +6,54 @@ import { AuthProviderHandlers, AuthResponse } from './../types'; export const provider = { makeStrategy(options: any): passport.Strategy { - return new GoogleStrategy(options, function ( - _req: any, - accessToken: any, - refreshToken: any, - profile: any, - cb: any, - ) { - cb(undefined, { profile, accessToken, refreshToken }); - }); + return new GoogleStrategy( + { ...options, passReqToCallback: true }, + function ( + _req: any, + accessToken: any, + refreshToken: any, + profile: any, + cb: any, + ) { + cb(undefined, { profile, accessToken, refreshToken }); + }, + ); }, - makeRouter(handlers: AuthProviderHandlers): express.Router { - const router = Router(); - router.get('/start', handlers.start); - router.get('/handler/frame', handlers.handle); - router.get('/logout', handlers.logout); - if (handlers.refresh) { - router.get('/refreshToken', handlers.refresh); - } - return router; + makeRouter(): express.Router { + return defaultRouter(GoogleAuthProviderHandler); }, }; +const defaultRouter = (handlers: AuthProviderHandlers) => { + const router = Router(); + router.get('/start', handlers.start); + router.get('/handler/frame', handlers.handle); + router.get('/logout', handlers.logout); + if (handlers.refresh) { + router.get('/refreshToken', handlers.refresh); + } + return router; +}; + +// Make this a class +// add a getStrategy method +// pass the config as a new constructor + +// class GoogleAuthProvider implements AuthProviderHandlers { +// config: any; +// constructor(config: any) { +// this.config = config; +// } + +// } + export const GoogleAuthProviderHandler: AuthProviderHandlers = { start(req, res, next) { + const scopes = req.query.scopes?.toString().split(','); return passport.authenticate('google', { - scope: ['profile', 'email'], + scope: scopes, accessType: 'offline', prompt: 'consent', - state: '8745634875963', })(req, res, next); }, handle(req, res, next) { diff --git a/plugins/auth-backend/src/providers/index.ts b/plugins/auth-backend/src/providers/index.ts index 3cb2b6a018..7bfd10224e 100644 --- a/plugins/auth-backend/src/providers/index.ts +++ b/plugins/auth-backend/src/providers/index.ts @@ -1,8 +1,5 @@ import { AuthProvider } from './types'; -import { - provider as GoogleAuthProvider, - GoogleAuthProviderHandler, -} from './google/provider'; +import { provider as GoogleAuthProvider } from './google/provider'; const providerFactories: AuthProvider = { google: GoogleAuthProvider, @@ -12,6 +9,6 @@ export const makeProvider = (config: any) => { const provider = config.provider; const providerFactory = providerFactories[provider]; const strategy = providerFactory.makeStrategy(config.options); - const providerRouter = providerFactory.makeRouter(GoogleAuthProviderHandler); + const providerRouter = providerFactory.makeRouter(); return { provider, strategy, providerRouter }; }; diff --git a/plugins/auth-backend/src/providers/types.ts b/plugins/auth-backend/src/providers/types.ts index 442ab405e2..d76b1c74fe 100644 --- a/plugins/auth-backend/src/providers/types.ts +++ b/plugins/auth-backend/src/providers/types.ts @@ -27,7 +27,7 @@ export type AuthProviderHandlers = { export type AuthProvider = { [key: string]: { makeStrategy(options: any): passport.Strategy; - makeRouter(handlers: AuthProviderHandlers): express.Router; + makeRouter(): express.Router; }; };