admin管理员组

文章数量:1323226

I'm trying to know if a user is banned from my middleware and redirect it to a custom error page. The problems happens when I make the query to the DB from Drizzle ORM, I have created an async function to select the user sanction but at the moment of use the value returned from my function the server throw this cryptic error:

 GET / 404 in 52ms
 GET /favicon.ico 200 in 1909ms
 ✓ Compiled middleware in 217ms
 ⨯ [Error [TypeError]: Unsupported type of value]
 ✓ Compiled in 20ms
 GET / 404 in 31ms
 GET /favicon.ico 200 in 431ms

this is my middleware:

import { auth } from "@/auth"
import type { NextRequest } from "next/server"
import { checkIsBanned } from "./lib/utils"


export const config = {
  matcher: ["/feed(.*)", "/posts(.*)", "/shop(.*)", "/"],
}

export async function middleware(req: NextRequest) {
  const userSession = await auth();
  const userIsBanned = await checkIsBanned(userSession?.user?.id as string)

  if (userIsBanned) {
    console.log("user is banned")
    const bannedPage = new URL("/banned", req.nextUrl.origin);
    return Response.redirect(bannedPage);
  }
}

and the function to check if the user is banned:

import { db } from "@/db";
import { type SelectSanction, sanctions } from "@/db/schema";
import { and, eq, lt } from "drizzle-orm";

export async function checkIsBanned(userId: string) {
  const currentDate = new Date()
  const sanctionsApplied: SelectSanction[] = await db.select()
    .from(sanctions)
    .where(and(eq(sanctions.userId, userId), lt(sanctions.endDate, currentDate)));

  console.log(sanctionsApplied)
  return sanctionsApplied.length > 0;
}

I have tried to find info in google but it seems to be a particular case and just expect to receive a boolean indicating if the user is banned or not

I'm using Next 15 + drizzleORM + libsql

本文标签: typescriptUnsupported type of value in NextjsStack Overflow