This is a simple, self-contained backend service built with FastAPI that demonstrates a basic user registration and email verification flow. It includes endpoints to register a new user and to handle a verification link sent to the user's email.
NOTE: This code uses an in-memory dictionary as a temporary database and simulates email sending by printing the verification link to the console. It is intended as a starting point and should be adapted for a production environment.
-
User Registration: An endpoint to create a new user account with an email and password.
-
Password Hashing: Securely hashes user passwords using
bcrypt
. -
Verification Tokens: Generates a unique, URL-safe token for email verification.
-
Email Verification Endpoint: A
GET
endpoint that marks a user's email as verified when they click the link from their email. -
In-Memory "Database": A simple
dict
to store user data for demonstration purposes.
-
Clone the repository or save the code: Save the provided Python code as
main.py
in a new directory. -
Create a virtual environment (recommended):
python -m venv venv source venv/bin/activate # On macOS/Linux venv\Scripts\activate # On Windows
-
Install dependencies: This project requires
fastapi
,uvicorn
, andpasslib
. You can install them usingpip
:pip install "fastapi[all]" uvicorn "passlib[bcrypt]"
Once you have installed the dependencies, you can start the server by running the following command in your terminal:
uvicorn main:app --reload
The server will be accessible at http://127.0.0.1:8000
. The --reload
flag will automatically restart the server whenever you make changes to the code.
You can interact with the API using a tool like cURL, Postman, or by simply using your web browser.
URL: GET /
Description: A simple test endpoint to confirm the API is running.
Example Response:
{
"message": "Welcome to the FastAPI Email Verification Service!"
}
URL: POST /register
Description: Registers a new user.
Request Body:
{
"email": "testuser@example.com",
"password": "securepassword123"
}
Example Response:
{
"message": "User testuser@example.com registered successfully. Check your email for a verification link."
}
After a successful registration, the verification link will be printed to your terminal.
URL: GET /verify-email/{token}
Description: Verifies a user's email using the token from the verification link.
Example: Assuming the verification token is ab12c3d4e5f6g7h8...
, you would open a browser to:
http://127.0.0.1:8000/verify-email/ab12c3d4e5f6g7h8...
Example Response:
{
"message": "Email for user testuser@example.com has been successfully verified!"
}
To make this application production-ready, you would need to:
-
Replace the
fake_db
: Integrate a real database (e.g., RDS or DynamoDB, as per your project plan) and define the schema for user data. -
Implement a real email service: Replace the
print()
statement with a call to a service like SendGrid, Mailgun, or another email API. -
Add token expiration: Implement a mechanism to invalidate verification tokens after a certain period to prevent abuse.
-
Enhance security: Add rate limiting to prevent spam and brute-force attacks on the registration endpoint.