Overview
Laravel Breeze Next.js uses the Next.js 16 App Router with route groups to separate authenticated and guest routes.Route Structure
Route Groups
Route groups organize routes without affecting the URL structure. They’re created using parentheses:(groupName).
Why Route Groups?
- Separation of concerns - Keep authenticated and guest routes separate
- Shared layouts - Apply different layouts to different route groups
- Middleware logic - Handle authentication at the layout level
- No URL impact - The parentheses don’t appear in the URL
Root Layout
The root layout wraps all pages and sets up the HTML structure:src/app/layout.tsx
Authenticated Routes
Authenticated Layout
The(authenticated) group has its own layout that checks authentication and displays navigation:
src/app/(authenticated)/layout.tsx
- Uses
'use client'directive for client-side rendering - Calls
useAuthwithmiddleware: 'auth'to protect routes - Renders the
Navigationcomponent with user data - Wraps page content in a styled container
Dashboard Page
Example of an authenticated page:src/app/(authenticated)/dashboard/page.tsx
Guest Routes
Guest routes don’t have a shared layout but use theAuthCard component for consistent styling.
Login Page
src/app/(guest)/login/page.tsx
- Uses
middleware: 'guest'to redirect authenticated users redirectIfAuthenticated: '/dashboard'specifies where to redirect- Formik handles form state and validation
- Yup provides schema validation
Register Page
Similar structure to login, with additional fields:src/app/(guest)/register/page.tsx
Dynamic Routes
Password Reset with Token
The password reset route uses dynamic segments:Navigation Between Routes
Using Link Component
Programmatic Navigation
Reading URL Parameters
Search Params
Dynamic Segments
Current Pathname
Check the current route for active states:Client vs Server Components
When to Use ‘use client’
Use the'use client' directive when your component:
- Uses React hooks (
useState,useEffect, etc.) - Handles browser events (
onClick,onChange, etc.) - Uses browser APIs (
localStorage,window, etc.) - Uses Next.js client hooks (
useRouter,useParams, etc.)
Server Components (Default)
Components without'use client' are Server Components by default. They:
- Run on the server
- Can directly access backend resources
- Have a smaller JavaScript bundle
- Cannot use hooks or browser APIs
Best Practices
- Use route groups to organize related routes
- Share layouts between routes in the same group
- Client components only when necessary for interactivity
- Type your components with TypeScript interfaces
- Validate forms with Yup schemas
- Handle errors gracefully with try-catch blocks
Next Steps
- Components - Build reusable UI components
- Authentication - Implement the useAuth hook
- API Integration - Make authenticated API calls