Skip to content

Master Rust backend development with Axum in this comprehensive tutorial. Learn user authentication, PostgreSQL integration, email verification, and secure APIs. Build scalable, production-ready apps using Rust. Source code and Postman collection available for easy testing.

License

Notifications You must be signed in to change notification settings

AarambhDevHub/rust-backend-axum

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Rust Backend with Axum, PostgreSQL, & Email Verification

Watch the video

This repository contains the source code for a fully functional backend application built with Rust, using the Axum framework. It includes user authentication, email verification, and a connection to a PostgreSQL database.

🛠️ Features

  • User Authentication: Register, login, password reset functionality.
  • Email Verification: Users receive an email to verify their accounts.
  • PostgreSQL Integration: Store and manage user data securely.
  • JWT Authentication: Secure API endpoints with JSON Web Tokens (JWT).
  • Middleware: Implement custom middleware for authentication.
  • Testing with Postman: A full Postman collection is provided to test all API endpoints.

🚀 Getting Started

Prerequisites

To run this project, you will need:

  • Rust installed on your machine.
  • PostgreSQL installed and running locally or remotely.
  • SQLx-CLI for database migrations.
  • Postman for testing API endpoints.

Installation

  1. Clone the repository:

    git clone https://github.com/AarambhDevHub/rust-backend-axum.git
    cd rust-backend-axum
  2. Install dependencies:

    cargo install --path .
  3. Set up PostgreSQL:

    Create a new database in PostgreSQL and update the .env file with your database URL.

    Example:

    DATABASE_URL=postgres://user:password@localhost/dbname
    
  4. Run migrations:

    sqlx migrate run
  5. Start the server:

    cargo run

    The server will be running on http://127.0.0.1:8000.

📬 Email Verification Setup

To enable email verification, you will need to configure an email service provider. Update the following environment variables in your .env file:

SMTP_SERVER=smtp.your-email-provider.com
SMTP_PORT=587
SMTP_USER=[email protected]
SMTP_PASSWORD=your-email-password

The application will send verification emails to users after registration.

🧪 API Testing with Postman

You can test all the API endpoints using the provided Postman collection. Download the Postman collection here and import it into Postman.

API Endpoints:

POST /api/auth/register: Register a new user

POST /api/auth/login: Login with an existing user

GET /api/auth/forgot-password: Request password reset

POST /api/auth/reset-password: Reset user password

GET /api/auth/verify: Verify email with token

GET /api/users/me: Get current user profile (JWT required)

⚙️ Configuration

The application requires a .env file for configuration. Below are the required environment variables:

# -----------------------------------------------------------------------------
# Database (PostgreSQL)
# -----------------------------------------------------------------------------
DATABASE_URL=postgresql://postgres:password@localhost:5432/axum_auth

# -----------------------------------------------------------------------------
# JSON Web Token Credentials
# -----------------------------------------------------------------------------
JWT_SECRET_KEY=my_ultra_secure_jwt_secret_key
JWT_MAXAGE=60

# -----------------------------------------------------------------------------
# SMTP Server Settings
# -----------------------------------------------------------------------------
SMTP_SERVER=smtp.your-email-provider.com
SMTP_PORT=587                     # Common ports: 587 (TLS), 465 (SSL), 25 (non-secure)
[email protected]
SMTP_PASSWORD=your_email_password
[email protected]

🎯 Future Enhancements

Add role-based access control (RBAC) for different user roles (admin, user). Improve security with additional layers like rate limiting and input validation. Expand API to include more features like user profiles, etc.

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

✨ Acknowledgements

Axum for building powerful, fast APIs in Rust.

PostgreSQL for reliable database management.

SQLx for async SQL in Rust.

Donations

If you find this project useful and would like to support its continued development, you can make a donation via Buy Me a Coffee.

Thank you for your support!

About

Master Rust backend development with Axum in this comprehensive tutorial. Learn user authentication, PostgreSQL integration, email verification, and secure APIs. Build scalable, production-ready apps using Rust. Source code and Postman collection available for easy testing.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published