英文:
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
).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论