๐ก๐ฒ๐ ๐.๐ท๐ ๐ฅ๐ผ๐๐๐ฒ ๐ฃ๐ฟ๐ผ๐๐ฒ๐ฐ๐๐ถ๐ผ๐ป ๐ช๐ถ๐๐ต๐ผ๐๐ ๐๐ถ๐ฏ๐ฟ๐ฎ๐ฟ๐ถ๐ฒ๐
Most think you need NextAuth or Clerk for route protection. You do not. Small projects only need a cookie check.
Next.js middleware runs before requests hit your page. It checks access.
- No login? Go home.
- Login? Go to dashboard.
- Check: The isLoggedIn cookie.
Create middleware.ts in your root folder. Use a matcher. Matchers stop slow loads on images and API routes.
Set the isLoggedIn cookie to true to log in. Set it to a past date to log out.
Warning: This is not for real data. Users edit cookies in DevTools. Use signed JWTs for production. Use this for speed.
No installs. Copy. Paste. Ship.
Source: https://dev.to/abdulrdeveloper/nextjs-middleware-route-protection-without-any-library-7ec