https://github.com/oguzhan18/rust_mongodb_auth
Simple Auth API written in Rust
https://github.com/oguzhan18/rust_mongodb_auth
authentication authentication-backend mongoatlas mongodb rust rust-auth rust-lang
Last synced: about 2 months ago
JSON representation
Simple Auth API written in Rust
- Host: GitHub
- URL: https://github.com/oguzhan18/rust_mongodb_auth
- Owner: oguzhan18
- Created: 2024-04-05T08:16:11.000Z (about 1 year ago)
- Default Branch: master
- Last Pushed: 2024-04-05T08:36:59.000Z (about 1 year ago)
- Last Synced: 2025-02-07T21:18:08.713Z (3 months ago)
- Topics: authentication, authentication-backend, mongoatlas, mongodb, rust, rust-auth, rust-lang
- Language: Rust
- Homepage: https://rust-mongodb-auth.onrender.com/register
- Size: 19.5 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Rust MongoDB Auth API Documentation
This API provides endpoints for user registration and login using Actix web framework and MongoDB.
## Endpoints
### Register
Registers a new user with the provided username and password.
- **URL**: `/register`
- **Method**: `POST`
- **Request Body**: JSON#### Request Body Parameters
| Parameter | Type | Description |
|-----------|--------|----------------------------|
| `username`| string | User's username |
| `password`| string | User's password |#### Responses
- **200 OK**: User registered successfully
- **500 Internal Server Error**: Failed to register user### Login
Authenticates a user with the provided username and password.
- **URL**: `/login`
- **Method**: `POST`
- **Request Body**: JSON#### Request Body Parameters
| Parameter | Type | Description |
|-----------|--------|----------------------------|
| `username`| string | User's username |
| `password`| string | User's password |#### Responses
- **200 OK**: Login successful
- **401 Unauthorized**: Invalid credentials
- **500 Internal Server Error**: Failed to login## Rust Code Overview
The API is implemented in Rust programming language using Actix web framework and MongoDB driver. Below are the key components of the Rust code:
- **User Struct**: Represents a user with `username` and `password` fields.
- **register Function**: Handles user registration by inserting user data into the MongoDB collection. It takes user data and the MongoDB collection as arguments and returns a HTTP response indicating the registration status.
- **login Function**: Handles user login by verifying user credentials against the MongoDB collection. It takes user data and the MongoDB collection as arguments and returns a HTTP response indicating the login status.
- **main Function**: Initializes the Actix web server, connects to the MongoDB database, selects the 'users' collection, and binds the routes for registration and login.
## Usage
1. Make sure Rust and MongoDB are installed on your system.
2. Run `cargo run` to start the API server.
3. Send POST requests to `/register` and `/login` endpoints using JSON body to register and login users.## MongoDB Configuration
Update the MongoDB connection URI in the main function with your MongoDB Atlas credentials and cluster information.
```rust
let client = Client::with_uri_str("mongodb+srv://:@/")
.await
.expect("Failed to initialize MongoDB client");
```Replace ``, ``, and with your MongoDB Atlas credentials and cluster information.
## For Support
For support, send an email to [email protected] or you are invited to our telegram channel https://t.me/vsform_tr
## Contact Me