recipes/email-verification
Email Verification Service
Email verification service with code generation and validation
Email Verification Service with Fiber
A clean architecture based email verification service that generates and validates verification codes.
Features
- Clean Architecture implementation
- In-memory verification code storage
- SMTP email service integration
- Code generation and hashing
- Configurable code expiration
- Thread-safe operations
Project Structure
email-verification/
βββ api/
β βββ handlers/ # HTTP handlers
βββ application/ # Application business logic
βββ domain/ # Domain models and interfaces
βββ infrastructure/ # External implementations
β βββ code/ # Code generation
β βββ email/ # SMTP service
β βββ repository/ # Data storage
βββ config/ # ConfigurationConfiguration
The application is configured via environment variables. Copy .env.example to .env and fill in your SMTP credentials:
cp .env.example .env| Variable | Required | Default | Description |
|---|---|---|---|
SMTP_HOST | No | smtp.gmail.com | SMTP server hostname |
SMTP_PORT | No | 587 | SMTP server port |
SMTP_USER | Yes | β | SMTP username / email address |
SMTP_PASS | Yes | β | SMTP password or app-password |
CODE_EXPIRATION | No | 1m | How long a code stays valid, a Go duration such as 5m |
The application exits with a fatal error on startup if SMTP_USER or SMTP_PASS are not set.
API Endpoints
| Method | URL | Description |
|---|---|---|
| POST | /verify/send/:email | Send verification code |
| POST | /verify/check/:email/:code | Verify the received code |
Both endpoints are rate limited to 5 requests per minute per client. Requesting a new code while the previous one is still valid returns an error, and an expired code is replaced by a new request.
Example Usage
- Send verification code:
curl -X POST http://localhost:3000/verify/send/user@example.com- Verify code:
curl -X POST http://localhost:3000/verify/check/user@example.com/123456Response Examples
Success:
{
"message": "Code verified successfully"
}Error:
{
"error": "invalid code"
}How to Run
- Copy
.env.exampleto.envand set your SMTP credentials. - Export the environment variables and run the application:
set -a; source .env; set +a
go run main.goDependencies
- Fiber v3
- Go 1.27+