@sqrzro/server 2.0.0-bz.22 → 2.0.0-bz.24

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.
Files changed (2) hide show
  1. package/dist/middleware.js +12 -7
  2. package/package.json +1 -1
@@ -14,27 +14,32 @@ function redirect(request, pathname = DEFAULT_REDIRECT) {
14
14
  }
15
15
  return applyHeaders(request, NextResponse.redirect(`${request.nextUrl.origin}${pathname}?r=${encodeURIComponent(getRelativeUrl(request.nextUrl))}`));
16
16
  }
17
+ /*
18
+ * When deployed to Vercel in a preview environment, we need to bypass the protection when fetching
19
+ * the session from the API.
20
+ */
21
+ function bypassProtection() {
22
+ const headers = new Headers();
23
+ if (process.env.VERCEL_PROTECTION_BYPASS) {
24
+ headers.append('x-vercel-protection-bypass', process.env.VERCEL_PROTECTION_BYPASS);
25
+ }
26
+ return { headers };
27
+ }
17
28
  export async function handleMiddleware(request, nextFn) {
18
- console.log('middleware', request.nextUrl.pathname);
19
29
  // If the URL is /api/session, we should just return the response, otherwise we end up in a loop
20
30
  if (request.nextUrl.pathname === '/api/session') {
21
- console.log('URL is /api/session, returning');
22
31
  return applyHeaders(request, nextFn ? nextFn() : NextResponse.next());
23
32
  }
24
- console.log('URL is not /api/session, continuing');
25
33
  const sessionID = request.cookies.get(process.env.AUTH_COOKIE_NAME || 'auth_session')?.value || '';
26
- console.log(sessionID);
27
34
  try {
28
- const session = await fetch(`${request.nextUrl.origin}/api/session?id=${sessionID}&pathname=${request.nextUrl.pathname}`);
35
+ const session = await fetch(`${request.nextUrl.origin}/api/session?id=${sessionID}&pathname=${request.nextUrl.pathname}`, bypassProtection());
29
36
  const json = (await session.json());
30
- console.log('json', json);
31
37
  if (json.redirect === null) {
32
38
  return applyHeaders(request, nextFn ? nextFn() : NextResponse.next());
33
39
  }
34
40
  return redirect(request, json.redirect);
35
41
  }
36
42
  catch (err) {
37
- console.log('error', err);
38
43
  return redirect(request);
39
44
  }
40
45
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sqrzro/server",
3
- "version": "2.0.0-bz.22",
3
+ "version": "2.0.0-bz.24",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "ISC",