小编Aay*_*ngh的帖子

Next.js 中间件错误:- [错误:边缘运行时不支持 Node.js 'crypto' 模块]

我是 next.js 和 typescript 的新手,我对这个问题感到非常沮丧,因为已经 4 天了,我找不到解决方案。如果有人知道原因和问题,请帮助我解决方案。这会很有帮助的。谢谢

-- 这是我的中间件.ts

import jwt from "jsonwebtoken";
import { NextResponse, NextRequest } from "next/server";
import clientPromise from "./database/db";

interface RequestWithUserId extends NextRequest {
  userId: string;
}

export async function middleware(req: RequestWithUserId) {
  try {
    const mongoClient = await clientPromise;
    const token = req.cookies.get("jwt")?.value;

    if (!token) {
      return NextResponse.redirect("http://localhost:3000/login");
    }

    const decodedToken: any = jwt.verify(token, process.env.SECRET_KEY);

    const userId = decodedToken._id;

    const checkUserExists = await mongoClient
      .db()
      .collection("user")
      .findOne({ _id: userId });

    if (!checkUserExists) {
      return …
Run Code Online (Sandbox Code Playgroud)

typescript next.js

2
推荐指数
1
解决办法
9251
查看次数

标签 统计

next.js ×1

typescript ×1