Skip to content

Commit

Permalink
fix: Auth route builder (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p authored Jan 13, 2023
1 parent 2294351 commit b5f6d7a
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import passport from 'passport';
import { Strategy as FacebookStrategy } from 'passport-facebook';
import { ConfigModule, MedusaContainer } from '@medusajs/medusa/dist/types/global';
import { Router } from 'express';
Expand Down Expand Up @@ -53,10 +52,12 @@ export function getFacebookAdminAuthRouter(facebook: FacebookAuthOptions, config
authPath: facebook.admin.authPath ?? '/admin/auth/facebook',
authCallbackPath: facebook.admin.authCallbackPath ?? '/admin/auth/facebook/cb',
successRedirect: facebook.admin.successRedirect,
failureRedirect: facebook.admin.failureRedirect,
passportAuthenticateMiddleware: passport.authenticate(FACEBOOK_ADMIN_STRATEGY_NAME, {
strategyName: FACEBOOK_ADMIN_STRATEGY_NAME,
passportAuthenticateMiddlewareOptions: {
scope: ['email'],
session: false,
})
},
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: facebook.admin.failureRedirect
}
});
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import passport from 'passport';
import { Router } from 'express';
import { ConfigModule, MedusaContainer } from '@medusajs/medusa/dist/types/global';
import { Strategy as FacebookStrategy } from 'passport-facebook';
Expand Down Expand Up @@ -54,10 +53,12 @@ export function getFacebookStoreAuthRouter(facebook: FacebookAuthOptions, config
authPath: facebook.store.authPath ?? '/store/auth/facebook',
authCallbackPath: facebook.store.authCallbackPath ?? '/store/auth/facebook/cb',
successRedirect: facebook.store.successRedirect,
failureRedirect: facebook.store.failureRedirect,
passportAuthenticateMiddleware: passport.authenticate(FACEBOOK_STORE_STRATEGY_NAME, {
strategyName: FACEBOOK_STORE_STRATEGY_NAME,
passportAuthenticateMiddlewareOptions: {
scope: ['email'],
session: false,
})
},
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: facebook.store.failureRedirect
}
});
}
11 changes: 6 additions & 5 deletions packages/medusa-plugin-auth/src/auth-strategies/google/admin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import passport from 'passport';
import { Strategy as GoogleStrategy } from 'passport-google-oauth2';
import { ConfigModule, MedusaContainer } from '@medusajs/medusa/dist/types/global';
import { Router } from 'express';
Expand Down Expand Up @@ -53,13 +52,15 @@ export function getGoogleAdminAuthRouter(google: GoogleAuthOptions, configModule
authPath: google.admin.authPath ?? '/admin/auth/google',
authCallbackPath: google.admin.authCallbackPath ?? '/admin/auth/google/cb',
successRedirect: google.admin.successRedirect,
failureRedirect: google.admin.failureRedirect,
passportAuthenticateMiddleware: passport.authenticate(GOOGLE_ADMIN_STRATEGY_NAME, {
strategyName: GOOGLE_ADMIN_STRATEGY_NAME,
passportAuthenticateMiddlewareOptions: {
scope: [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
],
session: false,
}),
},
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: google.admin.failureRedirect
}
});
}
11 changes: 6 additions & 5 deletions packages/medusa-plugin-auth/src/auth-strategies/google/store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import passport from 'passport';
import { Router } from 'express';
import { ConfigModule, MedusaContainer } from '@medusajs/medusa/dist/types/global';
import { Strategy as GoogleStrategy } from 'passport-google-oauth2';
Expand Down Expand Up @@ -53,13 +52,15 @@ export function getGoogleStoreAuthRouter(google: GoogleAuthOptions, configModule
authPath: google.store.authPath ?? '/store/auth/google',
authCallbackPath: google.store.authCallbackPath ?? '/store/auth/google/cb',
successRedirect: google.store.successRedirect,
failureRedirect: google.store.failureRedirect,
passportAuthenticateMiddleware: passport.authenticate(GOOGLE_STORE_STRATEGY_NAME, {
strategyName: GOOGLE_STORE_STRATEGY_NAME,
passportAuthenticateMiddlewareOptions: {
scope: [
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile',
],
session: false,
}),
},
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: google.admin.failureRedirect
}
});
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import passport from 'passport';
import { Strategy as LinkedinStrategy } from 'passport-linkedin-oauth2';
import { ConfigModule, MedusaContainer } from '@medusajs/medusa/dist/types/global';
import { Router } from 'express';
Expand Down Expand Up @@ -55,13 +54,15 @@ export function getLinkedinAdminAuthRouter(linkedin: LinkedinAuthOptions, config
authPath: linkedin.admin.authPath ?? '/admin/auth/linkedin',
authCallbackPath: linkedin.admin.authCallbackPath ?? '/admin/auth/linkedin/cb',
successRedirect: linkedin.admin.successRedirect,
failureRedirect: linkedin.admin.failureRedirect,
passportAuthenticateMiddleware: passport.authenticate(LINKEDIN_ADMIN_STRATEGY_NAME, {
strategyName: LINKEDIN_ADMIN_STRATEGY_NAME,
passportAuthenticateMiddlewareOptions: {
scope: [
'https://www.linkedinapis.com/auth/userinfo.email',
'https://www.linkedinapis.com/auth/userinfo.profile',
],
session: false,
}),
},
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: linkedin.admin.failureRedirect
}
});
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import passport from 'passport';
import { Router } from 'express';
import { ConfigModule, MedusaContainer } from '@medusajs/medusa/dist/types/global';
import { Strategy as LinkedinStrategy } from 'passport-linkedin-oauth2';
import { PassportStrategy } from '../../core/passport/Strategy';
import { LINKEDIN_STORE_STRATEGY_NAME, LinkedinAuthOptions, Profile } from './types';
import { validateStoreCallback } from "../../core/validate-callback";
import { passportAuthRoutesBuilder } from "../../core/passport/utils/auth-routes-builder";
import { validateStoreCallback } from '../../core/validate-callback';
import { passportAuthRoutesBuilder } from '../../core/passport/utils/auth-routes-builder';

export class LinkedinStoreStrategy extends PassportStrategy(LinkedinStrategy, LINKEDIN_STORE_STRATEGY_NAME) {
constructor(
Expand Down Expand Up @@ -55,13 +54,15 @@ export function getLinkedinStoreAuthRouter(linkedin: LinkedinAuthOptions, config
authPath: linkedin.store.authPath ?? '/store/auth/linkedin',
authCallbackPath: linkedin.store.authCallbackPath ?? '/store/auth/linkedin/cb',
successRedirect: linkedin.store.successRedirect,
failureRedirect: linkedin.store.failureRedirect,
passportAuthenticateMiddleware: passport.authenticate(LINKEDIN_STORE_STRATEGY_NAME, {
strategyName: LINKEDIN_STORE_STRATEGY_NAME,
passportAuthenticateMiddlewareOptions: {
scope: [
'https://www.linkedinapis.com/auth/userinfo.email',
'https://www.linkedinapis.com/auth/userinfo.profile',
],
session: false,
}),
},
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: linkedin.store.failureRedirect
}
});
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { RequestHandler, Router } from 'express';
import { Router } from 'express';
import passport from 'passport';
import cors from 'cors';
import { GOOGLE_ADMIN_STRATEGY_NAME } from '../../../auth-strategies/google';
import { TWENTY_FOUR_HOURS_IN_MS } from '../../../types';
import { authCallbackMiddleware } from '../../auth-callback-middleware';
import { ConfigModule } from '@medusajs/medusa/dist/types/global';
Expand All @@ -10,31 +9,34 @@ import { ConfigModule } from '@medusajs/medusa/dist/types/global';
* Build and return a router including the different route and configuration for a passport strategy
* @param domain
* @param configModule
* @param authPath
* @param passportAuthenticateMiddleware
* @param authPath The path used to start the auth process e.g /admin/auth/google
* @param authCallbackPath The pass used as the callback handler
* @param strategyName The name use the define the strategy
* @param passportAuthenticateMiddlewareOptions The options apply to the passport strategy on the auth path
* @param passportCallbackAuthenticateMiddlewareOptions The options apply to the passport strategy on the callback auth path
* @param expiresIn
* @param successRedirect
* @param authCallbackPath
* @param failureRedirect
*/
export function passportAuthRoutesBuilder({
domain,
configModule,
authPath,
passportAuthenticateMiddleware,
strategyName,
passportAuthenticateMiddlewareOptions,
passportCallbackAuthenticateMiddlewareOptions,
expiresIn,
successRedirect,
authCallbackPath,
failureRedirect,
}: {
domain: "admin" | "store",
configModule: ConfigModule;
authPath: string;
passportAuthenticateMiddleware: RequestHandler<any>;
strategyName: string;
passportAuthenticateMiddlewareOptions: Record<string, unknown>;
passportCallbackAuthenticateMiddlewareOptions: Record<string, unknown>;
expiresIn?: number;
successRedirect: string;
authCallbackPath: string;
failureRedirect?: string;
}): Router {
const router = Router();

Expand All @@ -46,7 +48,10 @@ export function passportAuthRoutesBuilder({
router.get(authPath, cors(adminCorsOptions));
/*necessary if you are using non medusajs client such as a pure axios call, axios initially requests options and then get*/
router.options(authPath, cors(adminCorsOptions));
router.get(authPath, passportAuthenticateMiddleware);
router.get(authPath, passport.authenticate(strategyName, {
...passportAuthenticateMiddlewareOptions,
session: false,
}));

const callbackHandler = authCallbackMiddleware(
domain,
Expand All @@ -65,8 +70,8 @@ export function passportAuthRoutesBuilder({

next();
},
passport.authenticate(GOOGLE_ADMIN_STRATEGY_NAME, {
failureRedirect,
passport.authenticate(strategyName, {
...passportCallbackAuthenticateMiddlewareOptions,
session: false,
}),
callbackHandler
Expand Down

0 comments on commit b5f6d7a

Please sign in to comment.