From fa49e04e54d96f28332a857fc06009fe5ecd6ba4 Mon Sep 17 00:00:00 2001 From: David Date: Wed, 1 Nov 2023 20:49:40 +0100 Subject: [PATCH] fixed errors --- .../src/auth-strategies/firebase/utils.ts | 4 ++-- .../src/core/auth-callback-middleware.ts | 2 +- .../src/core/passport/utils/auth-routes-builder.ts | 12 ++++++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/medusa-plugin-auth/src/auth-strategies/firebase/utils.ts b/packages/medusa-plugin-auth/src/auth-strategies/firebase/utils.ts index 6aeda86..6b25aff 100644 --- a/packages/medusa-plugin-auth/src/auth-strategies/firebase/utils.ts +++ b/packages/medusa-plugin-auth/src/auth-strategies/firebase/utils.ts @@ -4,7 +4,7 @@ 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, expiresIn?: string | number) { +function firebaseCallbackMiddleware(domain: 'admin' | 'store', configModule: ConfigModule, expiresIn?: number) { return (req: Request, res: Response) => { console.log(req.query); if(req.query.returnAccessToken == 'true') { @@ -31,7 +31,7 @@ export function firebaseAuthRoutesBuilder({ configModule: ConfigModule; authPath: string; strategyName: string; - expiresIn?: string | number; + expiresIn?: number; }): Router { const router = Router(); diff --git a/packages/medusa-plugin-auth/src/core/auth-callback-middleware.ts b/packages/medusa-plugin-auth/src/core/auth-callback-middleware.ts index 62c4502..03919d3 100644 --- a/packages/medusa-plugin-auth/src/core/auth-callback-middleware.ts +++ b/packages/medusa-plugin-auth/src/core/auth-callback-middleware.ts @@ -15,7 +15,7 @@ export function authCallbackMiddleware( }; } -export function signToken(domain: 'admin' | 'store', configModule: ConfigModule, user: any, expiresIn?: string | number) { +export function signToken(domain: 'admin' | 'store', configModule: ConfigModule, user: any, expiresIn?: number) { if(domain === 'admin') { return jwt.sign( { user_id: user.id, domain: 'admin' }, diff --git a/packages/medusa-plugin-auth/src/core/passport/utils/auth-routes-builder.ts b/packages/medusa-plugin-auth/src/core/passport/utils/auth-routes-builder.ts index 6aaa21b..3147692 100644 --- a/packages/medusa-plugin-auth/src/core/passport/utils/auth-routes-builder.ts +++ b/packages/medusa-plugin-auth/src/core/passport/utils/auth-routes-builder.ts @@ -45,7 +45,7 @@ export function passportAuthRoutesBuilder({ passportCallbackAuthenticateMiddlewareOptions: PassportCallbackAuthenticateMiddlewareOptions; successRedirect: string; authCallbackPath: string; - expiresIn?: number | string; + expiresIn?: number; }): Router { const router = Router(); @@ -67,7 +67,7 @@ export function passportAuthRoutesBuilder({ authPath, (req, res, next) => { // Allow to override the successRedirect by passing a query param `?redirectTo=your_url` - successAction = successActionHandlerFactory(req, domain, configModule, defaultRedirect); + successAction = successActionHandlerFactory(req, domain, configModule, defaultRedirect, expiresIn); next(); }, passport.authenticate(strategyName, { @@ -98,14 +98,15 @@ export function passportAuthRoutesBuilder({ return router; } -function successActionHandlerFactory(req: Request, domain: 'admin' | 'store', configModule: ConfigModule, defaultRedirect: string, expiresIn?: string | number) { +function successActionHandlerFactory(req: Request, domain: 'admin' | 'store', configModule: ConfigModule, defaultRedirect: string, expiresIn?: 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, expiresIn); + console.log(expiresIn); if(returnAccessToken) { return (req: Request, res: Response) => { + const token = signToken(domain, configModule, req.user, expiresIn); res.json({ access_token: token }); }; } else { @@ -113,6 +114,9 @@ function successActionHandlerFactory(req: Request, domain: 'admin' | 'store', co const authenticateSession = authenticateSessionFactory(domain); authenticateSession(req, res); + const token = signToken(domain, configModule, req.user, expiresIn); + + // append token to redirect url as query param const url = new URL(redirectUrl); url.searchParams.append('access_token', token);