Email Verification and Password Reset
Svelte Guardian provides built-in support for email verification and password reset functionality to enhance the security of your application.
Email Verification
Email verification ensures that users register with valid email addresses that they own. This prevents spam accounts and provides an additional layer of security.
Configuration
Configure email verification in your Svelte Guardian configuration:
Verification Methods
Svelte Guardian supports two verification methods:
One-Time Password (OTP)
- A numeric code is sent to the user’s email
- User enters the code on your verification page
- Simpler user experience for mobile users
Verification Link
- A unique URL with a token is sent to the user’s email
- User clicks the link to verify their email
- More familiar experience for many users
Implementation
Server-Side Setup
The email verification endpoints are automatically set up by Svelte Guardian:
POST:/auth/verify-email/send-otp
: Sends a verification code or linkPOST:/auth/verify-email/verify-otp
: Verifies the OTP or token
Client-Side Implementation
Create a verification page that allows users to request and submit verification codes:
Verify Your Email
We need to verify your email address.
{:else}Enter Verification Code
We've sent a verification code to {email}.
{#if error}Password Reset
Password reset functionality allows users to regain access to their accounts when they forget their passwords.
Configuration
Configure password reset in your Svelte Guardian configuration:
How It Works
- User requests a password reset by providing their email
- A unique reset token is generated and stored
- A reset link containing the token is sent to the user’s email
- User clicks the link and is directed to your reset password page
- User enters a new password
- The system verifies the token and updates the password
Implementation
Server-Side Setup
The password reset endpoints are automatically set up by Svelte Guardian:
POST:/auth/reset-password/initiate-reset
: Initiates the password reset processPOST:/auth/reset-password/reset
: Handles the actual password reset
Client-Side Implementation
Create a reset password page:
Check Your Email
We've sent password reset instructions to {email}.
{:else if success && token}Password Reset Complete
Your password has been successfully reset. Redirecting to login...
{:else if token}Reset Your Password
{#if error}Reset Your Password
Enter your email address and we'll send you instructions to reset your password.
{#if error}Customizing Email Templates
You can customize the email templates for verification and password reset emails:
Your verification code is: {{otp}}
' }, passwordReset: { subject: 'Reset your password', textTemplate: 'Click this link to reset your password: {{url}}', htmlTemplate: 'Click here to reset your password.
' } } }Security Considerations
Token Expiration: Set appropriate expiration times for verification and reset tokens. Shorter times are more secure, but might inconvenience users.
Rate Limiting: Apply strict rate limits to verification and password reset endpoints to prevent abuse.
Email Sending Failures: Implement proper error handling for situations where emails cannot be sent.
Token Storage: Verification tokens are securely hashed before being stored in the database.
Password Requirements: Enforce strong password requirements when users set new passwords during reset.
Testing
For development and testing purposes, you might want to log emails instead of sending them:
This allows you to get verification codes and reset links without setting up an actual email service.