This is Part 3 in the Full-Stack Next.js Development with Nhost series. This part creates a production-ready authentication system with secure email verification and proper error handling using Next.js App Router.
Full-Stack Next.js Development with Nhost
1. Create Project
Set up your Nhost project
2. Protected Routes
Route protection basics
3. User Authentication
Current - Complete auth flow
4. GraphQL Operations
CRUD operations with GraphQL
5. File Uploads
File upload and management
Prerequisites
- Complete the Protected Routes part first
- The project from the previous part set up and running
Step-by-Step Guide
1
Create the Sign In Flow
In this step, we’ll create a complete sign-in flow using Next.js App Router patterns. We’ll build three key files: a server component for the main page that handles URL parameters, a client component for the interactive form, and server actions for secure authentication processing.The main sign-in page is a server component that handles URL parameters (like error messages) and renders the sign-in form. This component runs on the server and can access search parameters directly.
src/app/signin/page.tsx
2
Create the Sign Up Flow
In this step, we’ll build the user registration system with email verification support. The sign-up flow includes handling both the registration form and the email verification success state, all using Next.js server and client components.The sign-up page is a server component that manages different states: showing the registration form or displaying the email verification success message. It handles URL parameters to determine which state to render.
src/app/signup/page.tsx
3
Create the Email Verification System
In this step, we’ll implement email verification using Next.js Route Handlers. When users click the verification link in their email, it will process the token server-side and either redirect them to their profile or show an error page with debugging information.The verification Route Handler is a server-side API endpoint that processes email verification tokens. It validates the token, handles edge cases (like already signed-in users), and redirects appropriately based on the verification result.
src/app/verify/route.ts
Important Configuration Required: Before testing email verification, you must configure your Nhost project’s authentication settings:
- Go to your Nhost project dashboard
- Navigate to Settings → Authentication
- Add your local development URL (e.g.,
http://localhost:3000
) to the Allowed Redirect URLs field - Ensure your production domain is also added when deploying
redirectTo not allowed
error when users attempt to sign up or verify their email addresses.4
Create the Sign Out System
In this step, we’ll implement user sign-out functionality using Next.js patterns. We’ll create a client component for the sign-out button and a server action to handle the actual sign-out process securely on the server side.The sign-out button is a client component that provides an interactive button for users to sign out. It handles the user interaction and calls the server action, then manages navigation and component refresh after sign-out.
src/components/SignOutButton.tsx
5
Update Navigation Component
In this step, we’ll update the server-side navigation component that shows different links based on the user’s authentication state. The navigation will display “Sign In” and “Sign Up” links for unauthenticated users, and “Profile” and “Sign Out” for authenticated users.src/components/Navigation.tsx
6
Update Public Routes in Middleware
In this step, we’ll configure the middleware to allow access to authentication-related routes without requiring authentication. This ensures that users can access sign-in, sign-up, and email verification pages even when not logged in.src/middleware.ts
7
Run and Test the Application
Start your Next.js development server and test the complete authentication flow to ensure everything works properly.- Email Verification Flow: Try signing up with a new email address. Check your email for the verification link and click it. The verification route handler will process the token and redirect you to your profile.
- Sign In/Out Flow: Try signing out and then signing back in with the same credentials using the server actions.
- Server-Side Navigation: Notice how navigation links change based on authentication state - the navigation component is rendered server-side and shows different content based on the session.
- Route Protection: Try accessing protected routes while logged out to see the middleware-based protection in action.
- Cross-Tab Consistency: Open multiple tabs and test signing out from one tab. Unlike client-side React apps, you’ll need to refresh or navigate to see changes in other tabs due to server-side rendering.
Key Features Demonstrated
Server Components & Actions
Server Components & Actions
Full authentication flow using Next.js App Router with server components and server actions for secure, server-side processing.
Route Handlers
Route Handlers
Custom
/verify
Route Handler that securely processes email verification tokens server-side with proper error handling.Client/Server Separation
Client/Server Separation
Clear separation between server components for rendering and client components for interactivity, following Next.js best practices.
Error Handling
Error Handling
Comprehensive error handling with URL-based error states and dedicated error pages for different failure scenarios.
Session Management
Session Management
Server-side session handling with sign out functionality using server actions and proper state management.