From 5a103084b5d53a07913c70958a3fad892af08700 Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Tue, 24 Oct 2023 16:03:08 +0200 Subject: [PATCH] fix: failure redirect not working properly (#103) * fix: failure redirect not working properly * handle failure redirect not set --- packages/medusa-plugin-auth/package.json | 2 +- .../src/core/passport/Strategy.ts | 2 +- .../passport/utils/auth-routes-builder.ts | 26 +++++++++++++++---- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/packages/medusa-plugin-auth/package.json b/packages/medusa-plugin-auth/package.json index a09c698..1c6668b 100644 --- a/packages/medusa-plugin-auth/package.json +++ b/packages/medusa-plugin-auth/package.json @@ -1,6 +1,6 @@ { "name": "medusa-plugin-auth", - "version": "1.5.1", + "version": "1.6.0", "description": "Social authentication plugin for medusajs", "keywords": [ "social", diff --git a/packages/medusa-plugin-auth/src/core/passport/Strategy.ts b/packages/medusa-plugin-auth/src/core/passport/Strategy.ts index 01e9fb9..bc57cf0 100644 --- a/packages/medusa-plugin-auth/src/core/passport/Strategy.ts +++ b/packages/medusa-plugin-auth/src/core/passport/Strategy.ts @@ -19,7 +19,7 @@ export function PassportStrategy = any>( const validateResult = await this.validate(...params); done(null, validateResult); } catch (err) { - done(err, null); + done(null, null, { msg: err.message }); } }; 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 9c8d71c..f025b52 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 @@ -93,11 +93,27 @@ export function passportAuthRoutesBuilder({ next(); }, - passport.authenticate(strategyName, { - ...passportCallbackAuthenticateMiddlewareOptions, - session: false, - }), - callbackHandler + function (req, res, next) { + passport.authenticate( + strategyName, + Object.assign({}, passportCallbackAuthenticateMiddlewareOptions, { + session: false, + failureRedirect: false, + }), + (err, user, options) => { + if (options?.msg) { + if (passportCallbackAuthenticateMiddlewareOptions?.failureRedirect) { + return res.redirect( + passportCallbackAuthenticateMiddlewareOptions.failureRedirect + '?message=' + options.msg + ); + } else { + return res.status(401).json({ message: options.msg }); + } + } + return callbackHandler(req, res); + } + )(req, res, next); + } ); return router;