Clerk 在 Next.js 中未保护私有路由。

huangapple go评论85阅读模式
英文:

Clerk is not protecting private routes in Next js

问题

我在我的应用程序中使用Clerk进行身份验证。我已经使用中间件设置了受保护的路由,如下所示:

import { authMiddleware } from "@clerk/nextjs";

export default authMiddleware({
publicRoutes: ["/sign-in", "/sign-up"],
});

export const config = {
matcher: ["/((?!.\..|_next).)", "/", "/(api|trpc)(.)"],
};


没有登录的情况下,路由 `/` 不是公开的。但是我仍然可以访问它。我哪里做错了?
英文:

I'm using Clerk for authentication in my app. I've set protected routes using middleware like so:

import { authMiddleware } from "@clerk/nextjs";

export default authMiddleware({
  publicRoutes: ["/sign-in", "/sign-up"],
});

export const config = {
  matcher: ["/((?!.*\\..*|_next).*)", "/", "/(api|trpc)(.*)"],
};

The route / is not public without being signed in. Yet I can still navigate to it. Where am I going wrong?

答案1

得分: 1

我看到你已经搞清楚了,但对于其他有这个问题的人来说,根据文档middleware.ts 需要放在项目的根目录中。换句话说,如果你在 app 目录中使用它,它应该放在包含 app 目录的同一文件夹中(例如,C:\Users\[你的姓名]\[你的项目]\middleware.ts)。

英文:

I see that you figured it out, but for anyone else who has this issue, middleware.ts needs to be in the root of your project per the docs. In other words, if you're using the app directory, it would go in the same folder that contains the app directory (e.g., C:\Users\[your name]\[your project]\middleware.ts).

huangapple
  • 本文由 发表于 2023年6月19日 01:22:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76501752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定