Next.js, Authentication, project root #164312
-
BodyA few questions when implementing authentication with Next.js. Many tutorials refer to the root directory. This is sometimes not clear. So what is considered the root directory: Top-level project folder, src or app directory? And where are auth.ts, auth.config.ts and middleware.ts files placed in? Any links to Next.js Authentication project structure best practices or tutorials greatly appreciated. Thanks! Guidelines
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
|
Hello @Fred638, The root directory is the one you initialize with npm init which is usually the parent folder of next.config.js and package.json. There is no fixed structure it depends on the company you work at, some may follow ones like in tutorials, some might have their own. |
Beta Was this translation helpful? Give feedback.
-
|
Hello Fred638. In short, the root of your project is indeed the top-level project folder. Here are some GitHub repositories you can reference:
|
Beta Was this translation helpful? Give feedback.
-
|
Root Directory Auth File Placement Middleware must be in root to intercept all routes. |
Beta Was this translation helpful? Give feedback.
-
|
Great questions this confusion is super common when starting with authentication in Next.js, especially with newer versions using the 📁 What is the "root directory"? In most Next.js projects, the root directory is the top-level project folder (where 📂 Where to place common auth-related files?
/lib/auth.ts
/lib/auth.config.ts
/middleware.ts ✅
/src/middleware.ts ❌ (won’t work)✅ Best Practices :
🔗 Recommended Resources :
|
Beta Was this translation helpful? Give feedback.
Hello @Fred638,
The root directory is the one you initialize with npm init which is usually the parent folder of next.config.js and package.json.