Skip to content

Commit

Permalink
feat: Add support for dynamic success redirect url through a query param
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien2p committed Feb 17, 2023
1 parent f0c94eb commit 1d16fc1
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 16 deletions.
6 changes: 6 additions & 0 deletions docs/pages/authentication/auth0.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ newly added plugins. To do so here are the steps
admin: {
callbackUrl:`${BACKEND_URL}/admin/auth/auth0/cb`,
failureRedirect: `${ADMIN_URL}/login`,

// The success redirect can be overriden from the client by adding a query param `?redirectTo=your_url`
// This query param will have the priority over this configuration
successRedirect: `${ADMIN_URL}/`,
// authPath: '/admin/auth/auth0',
// authCallbackPath: '/admin/auth/auth0/cb',
Expand All @@ -55,6 +58,9 @@ newly added plugins. To do so here are the steps
store: {
callbackUrl:`${BACKEND_URL}/store/auth/auth0/cb`,
failureRedirect: `${STORE_URL}/login`,

// The success redirect can be overriden from the client by adding a query param `?redirectTo=your_url`
// This query param will have the priority over this configuration
successRedirect: `${STORE_URL}/`,
// authPath: '/store/auth/auth0',
// authCallbackPath: '/store/auth/auth0/cb',
Expand Down
6 changes: 6 additions & 0 deletions docs/pages/authentication/facebook.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ newly added plugins. To do so here are the steps
admin: {
callbackUrl:`${BACKEND_URL}/admin/auth/facebook/cb`,
failureRedirect: `${ADMIN_URL}/login`,

// The success redirect can be overriden from the client by adding a query param `?redirectTo=your_url`
// This query param will have the priority over this configuration
successRedirect: `${ADMIN_URL}/`,
// authPath: '/admin/auth/facebook',
// authCallbackPath: '/admin/auth/facebook/cb',
Expand All @@ -53,6 +56,9 @@ newly added plugins. To do so here are the steps
store: {
callbackUrl:`${BACKEND_URL}/store/auth/facebook/cb`,
failureRedirect: `${STORE_URL}/login`,

// The success redirect can be overriden from the client by adding a query param `?redirectTo=your_url`
// This query param will have the priority over this configuration
successRedirect: `${STORE_URL}/`,
// authPath: '/store/auth/facebook',
// authCallbackPath: '/store/auth/facebook/cb',
Expand Down
6 changes: 6 additions & 0 deletions docs/pages/authentication/google.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ newly added plugins. To do so here are the steps
admin: {
callbackUrl:`${BACKEND_URL}/admin/auth/google/cb`,
failureRedirect: `${ADMIN_URL}/login`,

// The success redirect can be overriden from the client by adding a query param `?redirectTo=your_url`
// This query param will have the priority over this configuration
successRedirect: `${ADMIN_URL}/`,
// authPath: '/admin/auth/google',
// authCallbackPath: '/admin/auth/google/cb',
Expand All @@ -53,6 +56,9 @@ newly added plugins. To do so here are the steps
store: {
callbackUrl:`${BACKEND_URL}/store/auth/google/cb`,
failureRedirect: `${STORE_URL}/login`,

// The success redirect can be overriden from the client by adding a query param `?redirectTo=your_url`
// This query param will have the priority over this configuration
successRedirect: `${STORE_URL}/`,
// authPath: '/store/auth/google',
// authCallbackPath: '/store/auth/google/cb',
Expand Down
6 changes: 6 additions & 0 deletions docs/pages/authentication/linkedin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ newly added plugins. To do so here are the steps
admin: {
callbackUrl:`${BACKEND_URL}/admin/auth/linkedin/cb`,
failureRedirect: `${ADMIN_URL}/login`,

// The success redirect can be overriden from the client by adding a query param `?redirectTo=your_url`
// This query param will have the priority over this configuration
successRedirect: `${ADMIN_URL}/`,
// authPath: '/admin/auth/linkedin',
// authCallbackPath: '/admin/auth/linkedin/cb',
Expand All @@ -53,6 +56,9 @@ newly added plugins. To do so here are the steps
store: {
callbackUrl:`${BACKEND_URL}/store/auth/linkedin/cb`,
failureRedirect: `${STORE_URL}/login`,

// The success redirect can be overriden from the client by adding a query param `?redirectTo=your_url`
// This query param will have the priority over this configuration
successRedirect: `${STORE_URL}/`,
// authPath: '/store/auth/linkedin',
// authCallbackPath: '/store/auth/linkedin/cb',
Expand Down
4 changes: 2 additions & 2 deletions packages/medusa-plugin-auth/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@
},
"peerDependencies": {
"@medusajs/medusa": "^1.7.0",
"passport": "0.4.0",
"passport": "^0.4.1",
"typeorm": "^0.2.45"
},
"devDependencies": {
"@medusajs/medusa": "^1.7.0",
"@types/express": "^4.17.14",
"@types/jest": "^29.1.2",
"jest": "^29.1.2",
"passport": "0.4.0",
"passport": "^0.4.1",
"ts-jest": "^29.0.3",
"ts-node": "^8.6.2",
"typeorm": "^0.2.45"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import jwt from 'jsonwebtoken';
* @param domain
* @param secret
* @param expiresIn
* @param successRedirect
* @param successRedirectGetter
*/
export function authCallbackMiddleware(
domain: 'admin' | 'store',
secret: string,
expiresIn: number,
successRedirect: string
successRedirectGetter: () => string
) {
return (req, res) => {
const sendToken = sendTokenFactory(domain, secret, expiresIn);
sendToken(req, res);
res.redirect(successRedirect);
res.redirect(successRedirectGetter());
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ export function passportAuthRoutesBuilder({
}): Router {
const router = Router();

const originalSuccessRedirect = successRedirect

const corsOptions = {
origin:
domain === 'admin'
Expand All @@ -59,10 +61,15 @@ export function passportAuthRoutesBuilder({
};

router.get(authPath, cors(corsOptions));
/*necessary if you are using non medusajs client such as a pure axios call, axios initially requests options and then get*/
/* 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));
router.get(
authPath,
(req, res, next) => {
// Allow to override the successRedirect by passing a query param `?redirectTo=your_url`
successRedirect = (req.query.redirectTo ? req.query.redirectTo : originalSuccessRedirect) as string
next()
},
passport.authenticate(strategyName, {
...passportAuthenticateMiddlewareOptions,
session: false,
Expand All @@ -73,7 +80,7 @@ export function passportAuthRoutesBuilder({
domain,
configModule.projectConfig.jwt_secret,
expiresIn ?? TWENTY_FOUR_HOURS_IN_MS,
successRedirect
() => successRedirect
);

router.get(authCallbackPath, cors(corsOptions));
Expand Down
10 changes: 1 addition & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14311,15 +14311,7 @@ passport-strategy@1.x.x, passport-strategy@^1.0.0:
resolved "https://registry.yarnpkg.com/passport-strategy/-/passport-strategy-1.0.0.tgz#b5539aa8fc225a3d1ad179476ddf236b440f52e4"
integrity sha512-CB97UUvDKJde2V0KDWWB3lyf6PC3FaZP7YxZ2G8OAtn9p4HI9j9JLP9qjOGZFvyl8uwNT8qM+hGnz/n16NI7oA==

passport@0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/passport/-/passport-0.4.0.tgz#c5095691347bd5ad3b5e180238c3914d16f05811"
integrity sha512-q6UQeX3ay/WkZmPwtc+Wi21NVPNggkbupE+swmsJsTptFi9cr6SiqUtfFkgfOth58gGKsooNSYajrEt9VF5IQg==
dependencies:
passport-strategy "1.x.x"
pause "0.0.1"

passport@^0.4.0:
passport@^0.4.0, passport@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/passport/-/passport-0.4.1.tgz#941446a21cb92fc688d97a0861c38ce9f738f270"
integrity sha512-IxXgZZs8d7uFSt3eqNjM9NQ3g3uQCW5avD8mRNoXV99Yig50vjuaez6dQK2qC0kVWPRTujxY0dWgGfT09adjYg==
Expand Down

0 comments on commit 1d16fc1

Please sign in to comment.