diff --git a/packages/medusa-plugin-auth/package.json b/packages/medusa-plugin-auth/package.json index edad2da..06b3710 100644 --- a/packages/medusa-plugin-auth/package.json +++ b/packages/medusa-plugin-auth/package.json @@ -79,7 +79,8 @@ "passport-firebase-jwt": "^1.2.1", "passport-google-oauth2": "^0.2.0", "passport-linkedin-oauth2": "^2.0.0", - "passport-oauth2": "^1.7.0" + "passport-oauth2": "^1.7.0", + "tldjs": "^2.3.1" }, "jest": { "preset": "ts-jest", diff --git a/packages/medusa-plugin-auth/src/core/passport/utils/__tests__/auth-route-builder.spec.ts b/packages/medusa-plugin-auth/src/core/passport/utils/__tests__/auth-route-builder.spec.ts deleted file mode 100644 index 3cb3774..0000000 --- a/packages/medusa-plugin-auth/src/core/passport/utils/__tests__/auth-route-builder.spec.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { extractDomain } from '../auth-routes-builder'; - -describe('auth route builder', () => { - it('should be able to extract a domain from an url', () => { - let url = 'https://www.google.com'; - let domain = extractDomain(url); - expect(domain).toBe('google.com'); - - url = 'http://www.google.com'; - domain = extractDomain(url); - expect(domain).toBe('google.com'); - - url = 'http://google.com'; - domain = extractDomain(url); - expect(domain).toBe('google.com'); - }); -}); 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 808cbc8..1673b2d 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 @@ -4,6 +4,7 @@ import cors from 'cors'; import { authCallbackMiddleware, authenticateSessionFactory, signToken } from '../../auth-callback-middleware'; import { ConfigModule } from '@medusajs/medusa/dist/types/global'; import { CookieOptions } from 'express-serve-static-core'; +import { getDomain } from 'tldjs' type PassportAuthenticateMiddlewareOptions = { [key: string]: unknown; @@ -15,11 +16,6 @@ type PassportCallbackAuthenticateMiddlewareOptions = { failureRedirect: string; }; -export const extractDomain = (url) => { - const domain = url.match(/^(?:https?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n]+)/im)[1]; - return domain; -}; - /** * Build and return a router including the different route and configuration for a passport strategy * @param domain @@ -133,7 +129,7 @@ function successActionHandlerFactory( const returnAccessToken = req.query.returnAccessToken == 'true'; const redirectUrl = (req.query.redirectTo ? req.query.redirectTo : defaultRedirect) as string; const isProdOrStaging = process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'staging'; - const originHost = isProdOrStaging ? req.get('referer') && extractDomain(req.get('referer')) : undefined; + const originHost = isProdOrStaging ? req.get('referer') && getDomain(req.get('referer')) : undefined; if (returnAccessToken) { return (req: Request, res: Response) => {