Skip to content

Commit

Permalink
added expires in functionality back
Browse files Browse the repository at this point in the history
  • Loading branch information
dPreininger committed Nov 1, 2023
1 parent 3a527b3 commit 295cb9c
Show file tree
Hide file tree
Showing 15 changed files with 24 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,6 @@ export function getAuth0AdminAuthRouter(auth0: Auth0Options, configModule: Confi
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: auth0.admin.failureRedirect,
},
expiresIn: auth0.admin.expiresIn,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,6 @@ export function getAuth0StoreAuthRouter(auth0: Auth0Options, configModule: Confi
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: auth0.store.failureRedirect,
},
expiresIn: auth0.store.expiresIn,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ export function getAzureAdminAuthRouter(azure: AzureAuthOptions, configModule: C
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: azure.admin.failureRedirect,
},
expiresIn: azure.admin.expiresIn,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@ export function getAzureStoreAuthRouter(azure: AzureAuthOptions, configModule: C
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: azure.store.failureRedirect,
},
expiresIn: azure.store.expiresIn,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ export function getFacebookAdminAuthRouter(facebook: FacebookAuthOptions, config
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: facebook.admin.failureRedirect,
},
expiresIn: facebook.admin.expiresIn,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,6 @@ export function getFacebookStoreAuthRouter(facebook: FacebookAuthOptions, config
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: facebook.store.failureRedirect,
},
expiresIn: facebook.store.expiresIn,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ export function getFirebaseAdminAuthRouter(firebase: FirebaseAuthOptions, config
configModule,
authPath: firebase.admin.authPath ?? '/admin/auth/firebase',
strategyName: FIREBASE_ADMIN_STRATEGY_NAME,
expiresIn: firebase.admin.expiresIn,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@ export function getFirebaseStoreAuthRouter(firebase: FirebaseAuthOptions, config
configModule,
authPath: firebase.store.authPath ?? '/store/auth/firebase',
strategyName: FIREBASE_STORE_STRATEGY_NAME,
expiresIn: firebase.store.expiresIn,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { ConfigModule } from '@medusajs/medusa/dist/types/global';
import { Router, Request, Response } from 'express';
import { authenticateSessionFactory, signToken } from '../../core/auth-callback-middleware';

function firebaseCallbackMiddleware(domain: 'admin' | 'store', configModule: ConfigModule) {
function firebaseCallbackMiddleware(domain: 'admin' | 'store', configModule: ConfigModule, expiresIn?: string | number) {
return (req: Request, res: Response) => {
console.log(req.query);
if(req.query.returnAccessToken == 'true') {
const token = signToken(domain, configModule, req.user);
const token = signToken(domain, configModule, req.user, expiresIn);
res.json({ access_token: token });
return;
} else {
Expand All @@ -25,11 +25,13 @@ export function firebaseAuthRoutesBuilder({
configModule,
authPath,
strategyName,
expiresIn,
}: {
domain: 'admin' | 'store';
configModule: ConfigModule;
authPath: string;
strategyName: string;
expiresIn?: string | number;
}): Router {
const router = Router();

Expand All @@ -45,7 +47,7 @@ export function firebaseAuthRoutesBuilder({
/*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(corsOptions));

const callbackHandler = firebaseCallbackMiddleware(domain, configModule);
const callbackHandler = firebaseCallbackMiddleware(domain, configModule, expiresIn);

router.get(
authPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ export function getGoogleAdminAuthRouter(google: GoogleAuthOptions, configModule
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: google.admin.failureRedirect,
},
expiresIn: google.admin.expiresIn,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,6 @@ export function getGoogleStoreAuthRouter(google: GoogleAuthOptions, configModule
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: google.store.failureRedirect,
},
expiresIn: google.store.expiresIn,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ export function getLinkedinAdminAuthRouter(linkedin: LinkedinAuthOptions, config
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: linkedin.admin.failureRedirect,
},
expiresIn: linkedin.store.expiresIn,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,6 @@ export function getLinkedinStoreAuthRouter(linkedin: LinkedinAuthOptions, config
passportCallbackAuthenticateMiddlewareOptions: {
failureRedirect: linkedin.store.failureRedirect,
},
expiresIn: linkedin.store.expiresIn,
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ export function authCallbackMiddleware(
};
}

export function signToken(domain: 'admin' | 'store', configModule: ConfigModule, user: any) {
export function signToken(domain: 'admin' | 'store', configModule: ConfigModule, user: any, expiresIn?: string | number) {
if(domain === 'admin') {
return jwt.sign(
{ user_id: user.id, domain: 'admin' },
configModule.projectConfig.jwt_secret,
{
expiresIn: '24h',
expiresIn: expiresIn ?? '24h',
}
);
} else {
return jwt.sign(
{ customer_id: user.id, domain: 'store' },
configModule.projectConfig.jwt_secret,
{
expiresIn: '30d',
expiresIn: expiresIn ?? '30d',
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function passportAuthRoutesBuilder({
passportCallbackAuthenticateMiddlewareOptions,
successRedirect,
authCallbackPath,
expiresIn,
}: {
domain: 'admin' | 'store';
configModule: ConfigModule;
Expand All @@ -44,6 +45,7 @@ export function passportAuthRoutesBuilder({
passportCallbackAuthenticateMiddlewareOptions: PassportCallbackAuthenticateMiddlewareOptions;
successRedirect: string;
authCallbackPath: string;
expiresIn?: number | string;
}): Router {
const router = Router();

Expand Down Expand Up @@ -96,11 +98,11 @@ export function passportAuthRoutesBuilder({
return router;
}

function successActionHandlerFactory(req: Request, domain: 'admin' | 'store', configModule: ConfigModule, defaultRedirect: string) {
function successActionHandlerFactory(req: Request, domain: 'admin' | 'store', configModule: ConfigModule, defaultRedirect: string, expiresIn?: string | number) {
const returnAccessToken = req.query.returnAccessToken == 'true';
const redirectUrl = (req.query.redirectTo ? req.query.redirectTo : defaultRedirect) as string;

const token = signToken(domain, configModule, req.user);
const token = signToken(domain, configModule, req.user, expiresIn);

if(returnAccessToken) {
return (req: Request, res: Response) => {
Expand Down

0 comments on commit 295cb9c

Please sign in to comment.