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

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,37 +1,24 @@
1
1
  import { createClient } from 'redis';
2
2
  let client = null;
3
3
  async function getClient() {
4
- console.log('Getting the Redis client');
5
4
  if (!process.env.REDIS_URL) {
6
- console.log('Redis URL is not defined. Access to the cache is not possible.');
7
5
  throw new Error('REDIS_URL is not defined. Access to the cache is not possible.');
8
6
  }
9
- console.log('Redis URL is defined', process.env.REDIS_URL);
10
7
  if (client) {
11
- console.log('Client already exists, using it');
12
8
  return client;
13
9
  }
14
- console.log('Creating client...');
15
10
  client = createClient({
16
11
  socket: {
17
12
  tls: true,
18
13
  },
19
14
  url: process.env.REDIS_URL,
20
15
  });
21
- console.log('Client created', client);
22
- client.on('error', (err) => {
23
- console.log('REDIS ERROR', err);
24
- });
25
- console.log('Connecting to the Redis server...');
26
16
  await client.connect();
27
- console.log('Connected to the Redis server');
28
17
  return client;
29
18
  }
30
19
  export async function getFromCache(key) {
31
- console.log('Getting from cache', key);
32
20
  return (await getClient()).get(key);
33
21
  }
34
22
  export async function setToCache(key, value) {
35
- console.log('Setting to cache', key, value);
36
23
  await (await getClient()).set(key, value);
37
24
  }
@@ -15,16 +15,26 @@ function redirect(request, pathname = DEFAULT_REDIRECT) {
15
15
  return applyHeaders(request, NextResponse.redirect(`${request.nextUrl.origin}${pathname}?r=${encodeURIComponent(getRelativeUrl(request.nextUrl))}`));
16
16
  }
17
17
  export async function handleMiddleware(request, nextFn) {
18
+ console.log('middleware', request.nextUrl.pathname);
19
+ // If the URL is /api/session, we should just return the response, otherwise we end up in a loop
20
+ if (request.nextUrl.pathname === '/api/session') {
21
+ console.log('URL is /api/session, returning');
22
+ return applyHeaders(request, nextFn ? nextFn() : NextResponse.next());
23
+ }
24
+ console.log('URL is not /api/session, continuing');
18
25
  const sessionID = request.cookies.get(process.env.AUTH_COOKIE_NAME || 'auth_session')?.value || '';
26
+ console.log(sessionID);
19
27
  try {
20
28
  const session = await fetch(`${request.nextUrl.origin}/api/session?id=${sessionID}&pathname=${request.nextUrl.pathname}`);
21
29
  const json = (await session.json());
30
+ console.log('json', json);
22
31
  if (json.redirect === null) {
23
32
  return applyHeaders(request, nextFn ? nextFn() : NextResponse.next());
24
33
  }
25
34
  return redirect(request, json.redirect);
26
35
  }
27
36
  catch (err) {
37
+ console.log('error', err);
28
38
  return redirect(request);
29
39
  }
30
40
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sqrzro/server",
3
- "version": "2.0.0-bz.20",
3
+ "version": "2.0.0-bz.22",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "license": "ISC",