Next.js Discord

Discord Forum

The edge runtime does not support Node.js 'net' module.

Answered
Arinji posted this in #help-forum
Open in Discord
Middleware
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";
import { getUserId } from "./lib/getUserID";

export async function middleware(request: NextRequest) {
  if (request.nextUrl.pathname.startsWith("/dash")) {
    const token = request.cookies.get("token")?.value;

    if (token) {
      try {
        const user = await getUserId(token);

        console.log(user);
        if (user.verified === 0) {
          return NextResponse.redirect(`${process.env.WEB_DOMAIN}/verify`);
        } else return NextResponse.next();
      } catch (e) {
        console.error(e);
        return NextResponse.redirect(`${process.env.WEB_DOMAIN}/login`);
      }
    } else {
      const nextPath = encodeURIComponent(request.nextUrl.pathname);
      return NextResponse.redirect(
        `${process.env.WEB_DOMAIN}/login?next=${nextPath}`
      );
    }
  } else NextResponse.next();
}


import { User } from "@/lib/types";
import { query } from "./query";
import { jwtVerify } from "jose";

export async function verifyJwt(jwt: string): Promise<any> {
  const secretKey: Uint8Array = new TextEncoder().encode(
    process.env.JWT_SECRET as string
  );
  const { payload } = await jwtVerify(jwt, secretKey, {
    algorithms: ["HS256"],
  });

  return payload;
}

export async function getUserId(jwt: string) {
  try {
    const payload: any = await verifyJwt(jwt);

    const queryUserResult = await query(`SELECT * FROM users WHERE id=?`, [
      payload.id,
    ]);
    console.log(queryUserResult);
    const data: Array<any> = Array.isArray(queryUserResult)
      ? queryUserResult
      : [];

    return data[0] as User;
  } catch (error) {
    console.log(error);
    throw new Error("Unauthorized");
  }
}


I get both of the parts logged out,
Answered by DirtyCajunRice | AppDir
you dont query your database in the middleware
View full answer

25 Replies

is your web domain different from your nextjs domain?
Nextjs domain?
Also hi The Dirty, long time no see
@Arinji Nextjs domain?
your middleware is in domain.tld
is web domain the same domain
or a different one
Well we domain is just http://localhost:3000/
Web
Coz middleware needs absolute urls right
So
then thats stupid. dont use an env var. the data is in the req object already
Oh but that wouldn't be causing the node error though right?
no, its just something silly you shouldnt do
the error is because you are trying to query your database from the middleware
@DirtyCajunRice | AppDir no, its just something silly you shouldnt do
Yea, Oki gonna fix it.. so how would I redirect? To like /login
@Arinji Yea, Oki gonna fix it.. so how would I redirect? To like /login
get the host and protocol from the request object and make the new url
you dont query your database in the middleware
Answer
period
you just check cookies/headers
Hmm ok
if the jwt is valid you let it through
it should be httpOnly so it cant be modified by the client anyway.