This is Part 3 in the Full-Stack SvelteKit Development with Nhost series. This part creates a production-ready authentication system with secure email verification and proper error handling.
Full-Stack SvelteKit 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
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 Page
Build a comprehensive sign-in form with proper error handling and loading states. This page handles user authentication and includes special logic for post-verification sign-in.src/routes/signin/+page.svelte
2
Create the Sign Up Page
Implement user registration with email verification flow. This page collects user information, creates accounts, and guides users through the email verification process.src/routes/signup/+page.svelte
3
Create the Email Verification Page
Build a dedicated verification page that processes email verification tokens. This page handles the verification flow when users click the email verification link.src/routes/verify/+page.svelte
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:5173
) 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
Update the Layout Component to Include New Routes
Configure your application’s routing structure to include the new authentication pages. In SvelteKit, routes are automatically created based on the file structure, so you’ll update the layout component to handle authentication state properly.src/routes/+layout.svelte
5
Run and Test the Application
Start your development server and test the complete authentication flow to ensure everything works properly.- Try signing up with a new email address. Check your email for the verification link and click it. See how you are sent to the verification page and then redirected to your profile.
- Try signing out and then signing back in with the same credentials.
- Notice how navigation links change based on authentication state showing “Sign In” and “Sign Up” when logged out, and “Profile” and “Sign Out” when logged in.
- Check how the homepage also reflects the authentication state with appropriate messages.
- Open multiple tabs and test signing out from one tab to see how other tabs respond. Now sign back in and see the changes propagate across tabs.
Key Features Demonstrated
Complete Registration Flow
Complete Registration Flow
Full email/password registration with proper form validation and user feedback using SvelteKit’s reactive state management.
Email Verification
Email Verification
Custom
/verify
route that securely processes email verification tokens using SvelteKit’s page stores and navigation utilities.Error Handling
Error Handling
Comprehensive error handling for unverified emails, failed authentication, and network issues with proper TypeScript error typing.
Visual Feedback
Visual Feedback
Loading states, success messages, and clear error displays throughout the authentication flow using Svelte’s reactive declarations.
Session Management
Session Management
Complete sign out functionality and proper session state management across the application using Svelte stores and cross-tab synchronization.