Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/aarambhdevhub/rust-backend-axum

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.
https://github.com/aarambhdevhub/rust-backend-axum

authentication authentication-backend axum axum-authentication axum-extractor axum-framework axum-middleware axum-router axum-rs axum-server letter mail rust rust-language server sqlx sqlx-cli sqlx-postgres uuid validation

Last synced: 5 days ago
JSON representation

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.

Awesome Lists containing this project

README

        

# Rust Backend with Axum, PostgreSQL, & Email Verification

[![Watch the video](https://img.youtube.com/vi/M0wi7V1rP4Y/maxresdefault.jpg)](https://youtu.be/M0wi7V1rP4Y)

This repository contains the source code for a fully functional backend application built with Rust, using the [Axum](https://github.com/tokio-rs/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](https://www.rust-lang.org/) installed on your machine.
- [PostgreSQL](https://www.postgresql.org/) installed and running locally or remotely.
- [SQLx-CLI](https://crates.io/crates/sqlx-cli) for database migrations.
- [Postman](https://www.postman.com/) for testing API endpoints.

### Installation

1. **Clone the repository:**

```bash
git clone https://github.com/AarambhDevHub/rust-backend-axum.git
cd rust-backend-axum
```

2. **Install dependencies:**

```bash
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:**

```bash
sqlx migrate run
```

5. **Start the server:**

```bash
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:

```env
SMTP_SERVER=smtp.your-email-provider.com
SMTP_PORT=587
[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](https://github.com/AarambhDevHub/rust-backend-axum/blob/main/postman_collection.json) 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](LICENSE) file for details.

## โœจ Acknowledgements

[Axum](https://github.com/tokio-rs/axum) for building powerful, fast APIs in Rust.

[PostgreSQL](https://www.postgresql.org/) for reliable database management.

[SQLx](https://github.com/launchbadge/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](https://buymeacoffee.com/aarambhdevhub).

Thank you for your support!