Next.js Discord

Discord Forum

middleware bcrypt

Answered
Black Phoebe posted this in #help-forum
Open in Discord
Black PhoebeOP
 ⨯ ./node_modules/.pnpm/@mapbox+node-pre-gyp@1.0.11/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <!doctype html>
| <html>
| <head>

Import trace for requested module:
./node_modules/.pnpm/@mapbox+node-pre-gyp@1.0.11/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html
./node_modules/.pnpm/@mapbox+node-pre-gyp@1.0.11/node_modules/@mapbox/node-pre-gyp/lib/ sync ^\.\/.*$
./node_modules/.pnpm/@mapbox+node-pre-gyp@1.0.11/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
./node_modules/.pnpm/bcrypt@5.1.1/node_modules/bcrypt/bcrypt.js
./src/lib/auth/provider.ts
./src/lib/auth/utils.ts
 âš  ./node_modules/.pnpm/@mapbox+node-pre-gyp@1.0.11/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js
Module not found: Can't resolve 'npm' in 'D:\panel\node_modules\.pnpm\@mapbox+node-pre-gyp@1.0.11\node_modules\@mapbox\node-pre-gyp\lib\util'

22 Replies

middleware run on edge runtime which does not support bcrypt
Black PhoebeOP
ok now I'm using bcryptjs now this comes:
 node_modules\.pnpm\redis-errors@1.2.0\node_modules\redis-errors\index.js (3:0) @ <unknown>
 ⨯ Cannot read properties of undefined (reading 'charCodeAt')
null
 â—‹ Compiling /not-found ...
 ✓ Compiled /not-found in 775ms (395 modules)
 âš  Fast Refresh had to perform a full reload. Read more: https://nextjs.org/docs/messages/fast-refresh-reload
 ⨯ node_modules\.pnpm\redis-errors@1.2.0\node_modules\redis-errors\index.js (3:0) @ <unknown>
 ⨯ Cannot read properties of undefined (reading 'charCodeAt')
null
 ⨯ node_modules\.pnpm\redis-errors@1.2.0\node_modules\redis-errors\index.js (3:0) @ <unknown>
 ⨯ Cannot read properties of undefined (reading 'charCodeAt')
null
are you using redis for nodejs?
Black PhoebeOP
yes I'm using ioredis
which require node api
are you using next-auth and want to handle authorization in middelware
Black PhoebeOP
no
ok
Black PhoebeOP
I want to handle auth in middleware but using a custom system
export const validateSession = async (): Promise<User | null> => {
  const cookie = cookies().get("SESSION");
  if (cookie === undefined) {
    return null;
  }
  const sessionId = cookie.value;

  if (!validator.isUUID(sessionId)) {
    return null;
  }

  const redis = createRedisInstance();

  const session = await redis.get(`session:${sessionId}`);

  if (session === null) {
    return null;
  }

  let userId = Number(session);
  if (isNaN(userId)) {
    return null;
  }

  const userInfo = await AUTH.info(userId);

  if (userInfo === null) {
    return null;
  }

  return {
    id: userId,
    email: userInfo.email,
    username: userInfo.username,
    first_name: userInfo.first_name,
    last_name: userInfo.last_name,
  };
};


middleware:
export async function middleware(request: NextRequest) {
  if (request.nextUrl.pathname.startsWith("/sign-in")) return;
  const user = validateSession();
  if (user === null)
    NextResponse.redirect("/sign-in", {
      status: 302,
    });
}
can you turn validateSession to a route handler and try to fetch it in middleware?
or use upstash redis
@Ray can you turn `validateSession` to a route handler and try to fetch it in middleware?
Black PhoebeOP
yeah worked but now when I redirect the styles are gone
redirect from middleware?
Black PhoebeOP
yeah
does refresh show the style?
Black PhoebeOP
ok nvm the styles are completely gone
yeah I think your middleware is blocking the css request
add the matcher in config
Answer
Black PhoebeOP
yeah works
thx