|
| 1 | +# Authentication Middleware |
| 2 | + |
| 3 | +Simple route protection for AssetsUp. Redirects unauthenticated users to `/signin` and preserves their intended destination. |
| 4 | + |
| 5 | +## Quick Start |
| 6 | + |
| 7 | +The middleware checks for an `auth-token` cookie and protects routes listed in the `PROTECTED` array. |
| 8 | + |
| 9 | +```typescript |
| 10 | +const PROTECTED = ['/dashboard', '/assets', '/departments', '/users']; |
| 11 | +const AUTH_PAGES = ['/signin', '/signup']; |
| 12 | +``` |
| 13 | + |
| 14 | +## How It Works |
| 15 | + |
| 16 | +1. Protected route + no token → Redirect to `/signin?redirect={pathname}` |
| 17 | +2. Auth page + has token → Redirect to `/dashboard` |
| 18 | +3. Everything else → Allow |
| 19 | + |
| 20 | +## Adding Routes |
| 21 | + |
| 22 | +**New protected route:** |
| 23 | + |
| 24 | +```typescript |
| 25 | +const PROTECTED = ['/dashboard', '/assets', '/reports']; // Add here |
| 26 | + |
| 27 | +export const config = { |
| 28 | + matcher: ['/dashboard/:path*', '/assets/:path*', '/reports/:path*'], // Add here |
| 29 | +}; |
| 30 | +``` |
| 31 | + |
| 32 | +**New auth page:** |
| 33 | + |
| 34 | +```typescript |
| 35 | +const AUTH_PAGES = ['/signin', '/signup', '/forgot-password']; // Add here |
| 36 | + |
| 37 | +export const config = { |
| 38 | + matcher: [..., '/forgot-password'], // Add here |
| 39 | +}; |
| 40 | +``` |
| 41 | + |
| 42 | +## Using Redirect in Login Page |
| 43 | + |
| 44 | +```typescript |
| 45 | +'use client'; |
| 46 | +import { useRouter, useSearchParams } from 'next/navigation'; |
| 47 | + |
| 48 | +export default function SignInPage() { |
| 49 | + const router = useRouter(); |
| 50 | + const redirectUrl = useSearchParams().get('redirect') || '/dashboard'; |
| 51 | + |
| 52 | + const handleLogin = async () => { |
| 53 | + // ... login logic |
| 54 | + router.push(redirectUrl); |
| 55 | + router.refresh(); // Important: refresh to update middleware state |
| 56 | + }; |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +## Setting the Auth Token |
| 61 | + |
| 62 | +```typescript |
| 63 | +// app/api/auth/login/route.ts |
| 64 | +import { cookies } from 'next/headers'; |
| 65 | + |
| 66 | +cookies().set('auth-token', token, { |
| 67 | + httpOnly: true, |
| 68 | + secure: process.env.NODE_ENV === 'production', |
| 69 | + sameSite: 'lax', |
| 70 | + maxAge: 60 * 60 * 24 * 7, // 7 days |
| 71 | + path: '/', |
| 72 | +}); |
| 73 | +``` |
| 74 | + |
| 75 | +## Logout |
| 76 | + |
| 77 | +```typescript |
| 78 | +// app/api/auth/logout/route.ts |
| 79 | +import { cookies } from 'next/headers'; |
| 80 | + |
| 81 | +cookies().delete('auth-token'); |
| 82 | +``` |
| 83 | + |
| 84 | +## Testing Checklist |
| 85 | + |
| 86 | +- [ ] `/dashboard` without token → redirects to `/signin?redirect=/dashboard` |
| 87 | +- [ ] `/dashboard` with token → loads successfully |
| 88 | +- [ ] `/signin` with token → redirects to `/dashboard` |
| 89 | +- [ ] Login → redirects to original destination |
0 commit comments