(在 nextjs v13.4 中)Cookie 进入 localhost,但不进入 railway.app 上部署的服务器

(In nextjs v13.4) Cookies being get in localhost but not to the deployed server on railway.app

提问人:Hasan 提问时间:11/8/2023 最后编辑:Hasan 更新时间:11/10/2023 访问量:20

问:

我使用 express js 使用我的服务器,使用 nextjs v13.4 使用我的字体结束

当服务器 localhost cookie 工作正常时,但在部署 railway.app 它不能像 nextjs middleware.js(req.cookies.has(“token”)) 那样在字体端工作,让我 undefiend/false(请注意,浏览器 cookie 令牌可见,它没问题)

这是我的 Express JS app.js CORS 实现代码

app.use(
  cors({
    origin: [
      "https://e-golap-admin.vercel.app",
      "https://e-golap.vercel.app",
      "http://localhost:3000",
      "http://localhost:3001",
    ],
    methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
    credentials: true,
  })
);
// app.set("trust proxy", 1);
// app.enable("trust proxy");
app.use(function (req, res, next) {
  res.header("Content-Type", "application/json;charset=UTF-8");
  res.header("Access-Control-Allow-Credentials", true);
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  next();
});

在这里,当用户登录时,我的express js,然后设置cookie代码

 Response.cookie("token", token, {
          domain: "e-golap.vercel.app/",
          // maxAge: 1000 * 60 * 60 * 24 * 30, // 1 month
          maxAge: 2592000, // 1 month
          httpOnly: false,
          sameSite: "none",
          path: "/",
          secure: process.env.NODE_ENV === "production",
        });

在这里,我的font-end(nextjs v13.4)middleware.js代码

export async function middleware(req, res) {
  // check login
  let token = req.cookies.has("token");
  if (req.nextUrl.pathname.startsWith("/login")) {
    if (!token) {
      return NextResponse.next();
    } else {
      return NextResponse.redirect(new URL("/", req.url));
    }
  }
  // check checkout && confirm order
  if (
    req.nextUrl.pathname.startsWith("/checkout") ||
    req.nextUrl.pathname.startsWith("/confirm-order") ||
    req.nextUrl.pathname.startsWith("/user-dashboard") ||
    req.nextUrl.pathname.startsWith("/user-dashboard/edit-profile") ||
    req.nextUrl.pathname.startsWith("/user-dashboard/orders/running-orders") ||
    req.nextUrl.pathname.startsWith("/user-dashboard/orders/delivery-orders") ||
    req.nextUrl.pathname.startsWith("/user-dashboard/orders/return-orders") ||
    req.nextUrl.pathname.startsWith("/user-dashboard/orders/cancel-orders")
  ) {
    let token = req.cookies.has("token");
    if (token) {
      return NextResponse.next();
    } else {
      return NextResponse.redirect(new URL("/login", req.url));
    }
  }
}

这里

let token = req.cookies.has("token")

让我错了,但是当服务器在本地主机上时,它可以完美地工作。

我想在服务器部署生产时。得到我

let token = req.cookies.has("token") 

获取结果 true

node.js express next.js cookie next.js13

评论


答: 暂无答案