@greatapps/common 1.1.298 → 1.1.299
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { NextResponse } from "next/server";
|
|
2
2
|
import { continueChain, stopChain } from "./types";
|
|
3
|
+
import { whitelabelService } from "../modules/whitelabel/services/whitelabel.service";
|
|
3
4
|
function isRouteEquals(pathname, routes) {
|
|
4
5
|
return routes.some((route) => pathname === route || pathname.startsWith(`${route}/`));
|
|
5
6
|
}
|
|
6
7
|
function createAuthMiddleware(options) {
|
|
7
|
-
return (request) => {
|
|
8
|
+
return async (request) => {
|
|
8
9
|
const { pathname } = request.nextUrl;
|
|
9
10
|
const token = process.env.DUMMY_AUTH_TOKEN || request.cookies.get("greatapps")?.value;
|
|
10
11
|
const isPublicRoute = isRouteEquals(pathname, options?.publicRoutes || []);
|
|
@@ -13,7 +14,10 @@ function createAuthMiddleware(options) {
|
|
|
13
14
|
return stopChain(NextResponse.redirect(new URL("/", request.url)));
|
|
14
15
|
}
|
|
15
16
|
if (!token && !isPublicRoute && !isAuthRoute) {
|
|
16
|
-
const
|
|
17
|
+
const host = request.headers.get("host") || "";
|
|
18
|
+
const whitelabel = await whitelabelService.getTokenByDomain(host).catch(() => null);
|
|
19
|
+
const accountsUrl = whitelabel?.accounts_url;
|
|
20
|
+
const loginUrl = accountsUrl ? new URL("/login", accountsUrl) : new URL("/login", request.url);
|
|
17
21
|
if (pathname !== "/") {
|
|
18
22
|
loginUrl.searchParams.set("redirect", pathname);
|
|
19
23
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/middlewares/create-auth-middleware.ts"],"sourcesContent":["import { NextRequest, NextResponse } from 'next/server';\r\n\r\nimport { continueChain, MiddlewareConfig, MiddlewareFunction, MiddlewareResult, stopChain } from './types';\r\n\r\nfunction isRouteEquals(pathname: string, routes: string[]): boolean {\r\n return routes.some((route) => pathname === route || pathname.startsWith(`${route}/`));\r\n}\r\n\r\ninterface CreateAuthMiddlewareOptions {\r\n publicRoutes?: string[];\r\n authRoutes?: string[];\r\n}\r\n\r\nexport function createAuthMiddleware(options?: CreateAuthMiddlewareOptions): MiddlewareFunction {\r\n return (request: NextRequest) => {\r\n const { pathname } = request.nextUrl;\r\n const token = process.env.DUMMY_AUTH_TOKEN || request.cookies.get('greatapps')?.value;\r\n const isPublicRoute = isRouteEquals(pathname, options?.publicRoutes || []);\r\n const isAuthRoute = isRouteEquals(pathname, options?.authRoutes || []);\r\n\r\n if (token && isAuthRoute) {\r\n return stopChain(NextResponse.redirect(new URL('/', request.url)));\r\n }\r\n\r\n if (!token && !isPublicRoute && !isAuthRoute) {\r\n const loginUrl = new URL('/login', request.url);\r\n\r\n if (pathname !== '/') {\r\n loginUrl.searchParams.set('redirect', pathname);\r\n }\r\n\r\n return stopChain(NextResponse.redirect(loginUrl));\r\n }\r\n\r\n return continueChain();\r\n }\r\n}"],"mappings":"AAAA,SAAsB,oBAAoB;AAE1C,SAAS,eAAuE,iBAAiB;
|
|
1
|
+
{"version":3,"sources":["../../src/middlewares/create-auth-middleware.ts"],"sourcesContent":["import { NextRequest, NextResponse } from 'next/server';\r\n\r\nimport { continueChain, MiddlewareConfig, MiddlewareFunction, MiddlewareResult, stopChain } from './types';\r\nimport { whitelabelService } from '../modules/whitelabel/services/whitelabel.service';\r\n\r\nfunction isRouteEquals(pathname: string, routes: string[]): boolean {\r\n return routes.some((route) => pathname === route || pathname.startsWith(`${route}/`));\r\n}\r\n\r\ninterface CreateAuthMiddlewareOptions {\r\n publicRoutes?: string[];\r\n authRoutes?: string[];\r\n}\r\n\r\nexport function createAuthMiddleware(options?: CreateAuthMiddlewareOptions): MiddlewareFunction {\r\n return async (request: NextRequest) => {\r\n const { pathname } = request.nextUrl;\r\n const token = process.env.DUMMY_AUTH_TOKEN || request.cookies.get('greatapps')?.value;\r\n const isPublicRoute = isRouteEquals(pathname, options?.publicRoutes || []);\r\n const isAuthRoute = isRouteEquals(pathname, options?.authRoutes || []);\r\n\r\n if (token && isAuthRoute) {\r\n return stopChain(NextResponse.redirect(new URL('/', request.url)));\r\n }\r\n\r\n if (!token && !isPublicRoute && !isAuthRoute) {\r\n const host = request.headers.get('host') || '';\r\n const whitelabel = await whitelabelService.getTokenByDomain(host).catch(() => null);\r\n const accountsUrl = whitelabel?.accounts_url;\r\n\r\n const loginUrl = accountsUrl\r\n ? new URL('/login', accountsUrl)\r\n : new URL('/login', request.url);\r\n\r\n if (pathname !== '/') {\r\n loginUrl.searchParams.set('redirect', pathname);\r\n }\r\n\r\n return stopChain(NextResponse.redirect(loginUrl));\r\n }\r\n\r\n return continueChain();\r\n }\r\n}"],"mappings":"AAAA,SAAsB,oBAAoB;AAE1C,SAAS,eAAuE,iBAAiB;AACjG,SAAS,yBAAyB;AAElC,SAAS,cAAc,UAAkB,QAA2B;AAClE,SAAO,OAAO,KAAK,CAAC,UAAU,aAAa,SAAS,SAAS,WAAW,GAAG,KAAK,GAAG,CAAC;AACtF;AAOO,SAAS,qBAAqB,SAA2D;AAC9F,SAAO,OAAO,YAAyB;AACrC,UAAM,EAAE,SAAS,IAAI,QAAQ;AAC7B,UAAM,QAAQ,QAAQ,IAAI,oBAAoB,QAAQ,QAAQ,IAAI,WAAW,GAAG;AAChF,UAAM,gBAAgB,cAAc,UAAU,SAAS,gBAAgB,CAAC,CAAC;AACzE,UAAM,cAAc,cAAc,UAAU,SAAS,cAAc,CAAC,CAAC;AAErE,QAAI,SAAS,aAAa;AACxB,aAAO,UAAU,aAAa,SAAS,IAAI,IAAI,KAAK,QAAQ,GAAG,CAAC,CAAC;AAAA,IACnE;AAEA,QAAI,CAAC,SAAS,CAAC,iBAAiB,CAAC,aAAa;AAC5C,YAAM,OAAO,QAAQ,QAAQ,IAAI,MAAM,KAAK;AAC5C,YAAM,aAAa,MAAM,kBAAkB,iBAAiB,IAAI,EAAE,MAAM,MAAM,IAAI;AAClF,YAAM,cAAc,YAAY;AAEhC,YAAM,WAAW,cACb,IAAI,IAAI,UAAU,WAAW,IAC7B,IAAI,IAAI,UAAU,QAAQ,GAAG;AAEjC,UAAI,aAAa,KAAK;AACpB,iBAAS,aAAa,IAAI,YAAY,QAAQ;AAAA,MAChD;AAEA,aAAO,UAAU,aAAa,SAAS,QAAQ,CAAC;AAAA,IAClD;AAEA,WAAO,cAAc;AAAA,EACvB;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { NextRequest, NextResponse } from 'next/server';
|
|
2
2
|
|
|
3
3
|
import { continueChain, MiddlewareConfig, MiddlewareFunction, MiddlewareResult, stopChain } from './types';
|
|
4
|
+
import { whitelabelService } from '../modules/whitelabel/services/whitelabel.service';
|
|
4
5
|
|
|
5
6
|
function isRouteEquals(pathname: string, routes: string[]): boolean {
|
|
6
7
|
return routes.some((route) => pathname === route || pathname.startsWith(`${route}/`));
|
|
@@ -12,7 +13,7 @@ interface CreateAuthMiddlewareOptions {
|
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
export function createAuthMiddleware(options?: CreateAuthMiddlewareOptions): MiddlewareFunction {
|
|
15
|
-
return (request: NextRequest) => {
|
|
16
|
+
return async (request: NextRequest) => {
|
|
16
17
|
const { pathname } = request.nextUrl;
|
|
17
18
|
const token = process.env.DUMMY_AUTH_TOKEN || request.cookies.get('greatapps')?.value;
|
|
18
19
|
const isPublicRoute = isRouteEquals(pathname, options?.publicRoutes || []);
|
|
@@ -23,7 +24,13 @@ export function createAuthMiddleware(options?: CreateAuthMiddlewareOptions): Mid
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
if (!token && !isPublicRoute && !isAuthRoute) {
|
|
26
|
-
const
|
|
27
|
+
const host = request.headers.get('host') || '';
|
|
28
|
+
const whitelabel = await whitelabelService.getTokenByDomain(host).catch(() => null);
|
|
29
|
+
const accountsUrl = whitelabel?.accounts_url;
|
|
30
|
+
|
|
31
|
+
const loginUrl = accountsUrl
|
|
32
|
+
? new URL('/login', accountsUrl)
|
|
33
|
+
: new URL('/login', request.url);
|
|
27
34
|
|
|
28
35
|
if (pathname !== '/') {
|
|
29
36
|
loginUrl.searchParams.set('redirect', pathname);
|