Intermediary step before moving to class

This commit is contained in:
Raghunandan
2020-05-19 12:45:35 +02:00
parent 422d85b67d
commit 79ace90811
4 changed files with 42 additions and 27 deletions
@@ -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,
},
},
];
@@ -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) {
+2 -5
View File
@@ -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 };
};
+1 -1
View File
@@ -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;
};
};