Skip to content

marySkito/email-verification

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

FastAPI Email Verification Service

Project Description

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.

Features

  • 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.

Installation & Setup

  1. Clone the repository or save the code: Save the provided Python code as main.py in a new directory.

  2. Create a virtual environment (recommended):

    python -m venv venv
    source venv/bin/activate  # On macOS/Linux
    venv\Scripts\activate     # On Windows
  3. Install dependencies: This project requires fastapi, uvicorn, and passlib. You can install them using pip:

    pip install "fastapi[all]" uvicorn "passlib[bcrypt]"

Running the Application

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.

API Endpoints

You can interact with the API using a tool like cURL, Postman, or by simply using your web browser.

1. Root Endpoint

URL: GET / Description: A simple test endpoint to confirm the API is running. Example Response:

{
  "message": "Welcome to the FastAPI Email Verification Service!"
}

2. User Registration

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.

3. Email Verification

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!"
}

Next Steps for a Production Environment

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages