A simple user authentication API built with Rust using the Axum web framework and PostgreSQL. This project demonstrates safe password hashing, user creation, and login verification using modern Rust tools and practices.
- User registration with hashed passwords using Argon2
- Login endpoint to verify credentials
- PostgreSQL database with automatic table creation
- Secure error handling and basic validation
- Built using async Rust with Axum and SQLx
- Axum - Web framework
- Tokio - Asynchronous runtime
- SQLx - Async PostgreSQL support
- Argon2 - Secure password hashing
- UUID - User ID generation
- Chrono - Date/time management
Registers a new user.
Body:
{
"username": "your_username",
"password": "your_password"
}Responses:
201 Created: User successfully created400 Bad Request: Username or password too short409 Conflict: Username already exists500 Internal Server Error: Failed to create user
Verifies login credentials.
Body:
{
"username": "your_username",
"password": "your_password"
}Responses:
200 OK: Verification successful401 Unauthorized: Invalid username or password500 Internal Server Error: Server error during verification
- Rust (latest stable)
- PostgreSQL
sqlx-cli(for migrations, optional)
git clone https://github.com/your-username/rust-auth-api.git
cd rust-auth-apiEdit main.rs if needed:
Server::new(
"localhost:3000",
"postgres://postgres:secret@localhost:5432/postgres"
)Ensure the PostgreSQL database is running and accessible.
cargo runThe API will be available at http://localhost:3000.
src/
├── main.rs # App entry point
├── server.rs # Server setup and database config
├── router.rs # Route definitions
├── handlers.rs # Endpoint logic
└── user.rs # Data models
Created automatically if it does not exist:
CREATE TABLE IF NOT EXISTS users (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
username TEXT UNIQUE NOT NULL,
hashed_password TEXT NOT NULL,
created_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT now(),
last_login_at TIMESTAMP WITH TIME ZONE NOT NULL
);This project is open-source and freely available for educational and development use.