25 lines
		
	
	
		
			781 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			25 lines
		
	
	
		
			781 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import { NextResponse } from 'next/server';
 | 
						|
import type { NextRequest } from 'next/server';
 | 
						|
 | 
						|
export function middleware(request: NextRequest) {
 | 
						|
  const token = request.cookies.get('habit-tracker-token');
 | 
						|
  const isAuthPage = request.nextUrl.pathname === '/welcome';
 | 
						|
  const isDashboard = request.nextUrl.pathname === '/dashboard';
 | 
						|
 | 
						|
  // If trying to access dashboard without token, redirect to welcome
 | 
						|
  if (isDashboard && !token) {
 | 
						|
    return NextResponse.redirect(new URL('/welcome', request.url));
 | 
						|
  }
 | 
						|
 | 
						|
  // If trying to access welcome page with token, redirect to dashboard
 | 
						|
  if (isAuthPage && token) {
 | 
						|
    return NextResponse.redirect(new URL('/dashboard', request.url));
 | 
						|
  }
 | 
						|
 | 
						|
  return NextResponse.next();
 | 
						|
}
 | 
						|
 | 
						|
export const config = {
 | 
						|
  matcher: ['/dashboard', '/welcome'],
 | 
						|
};
 |