28/04/2026
Recently আমি Next.js (App Router) দিয়ে একটি Hospital Management App-এর জন্য পুরো authentication system নিজে হাতে build করেছি।
শুরুতে ভাবছিলাম—
login, token, done.
কিন্তু বাস্তবটা একদম আলাদা ছিল।
এই journey-তে যেসব challenge face করেছি:
• login এ infinite redirect loop
• token না থাকলেও refresh API বারবার call হওয়া
• middleware/proxy একদমই run না করা (file placement issue)
• cookie set না হওয়া
• CSRF token generate করতে runtime problem
• server action এ আলাদা করে security handle করার দরকার পড়া
এই প্রতিটা problem আমাকে জোর করে বুঝিয়েছে—
authentication আসলে ভিতরে কীভাবে কাজ করে।
---
আমি যা যা implement করেছি:
• Access + Refresh Token system
Short-lived access token, long-lived refresh token (DB controlled)
• HTTP-only cookies
XSS attack থেকে token secure রাখার জন্য
• CSRF protection
Cookie + header match করে forged request block করা
• Custom fetchWithAuth flow
Access token expire হলে silent refresh
• Role-based access control
Manager, Lab Assistant, Receptionist — route wise restriction
• Middleware/Proxy level protection
Route guard (UI level protection)
• Server Action level security
Backend validation — final authority
• Layout fallback checks
Extra safety layer
---
সবচেয়ে বড় learning:
Security কখনো এক layer না।
এটা combination of:
Route protection + Token validation + Role check + Backend verification
একটা layer miss করলে system vulnerable হয়ে যায়।
---
আরেকটা জিনিস বুঝলাম—
Authentication build করার চেয়ে
debug করা বেশি শেখায়।
কারণ তখন বুঝতে হয়:
• browser কখন cookie পাঠায়
• token expire হলে কী হয়
• frontend আর backend এর মধ্যে trust কোথায়
• request flow আসলে কীভাবে কাজ করে
---
এটা শুধু একটা feature build না।
এটা আমার system design আর security নিয়ে ভাবার ধরণটাই change করে দিয়েছে।
---
এখন next step:
• Refresh token rotation
• Audit logs
• Fine-grained permission system
---
যারা নিজেরা authentication build করেছো, তোমরা বুঝবে—
এটা messy, frustrating… কিন্তু শেষ পর্যন্ত খুব rewarding।
তোমরা কী prefer করো?
Custom auth system, নাকি ready solution like NextAuth / Firebase?