Skip to content

oguzhan18/rust_mongodb_auth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

let client = Client::with_uri_str("mongodb+srv://<username>:<password>@<cluster>/<database>")
    .await
    .expect("Failed to initialize MongoDB client");

Replace <username>, <password>, and with your MongoDB Atlas credentials and cluster information.

For Support

For support, send an email to oguzhancart1@gmail.com or you are invited to our telegram channel https://t.me/vsform_tr

Contact Me

oguzhan1881 oguzhan18 oguzhancart o%c4%9fuzhan-%c3%a7art-b73405199/ oguzhan18 oguzhan_cart cart188 @oguzhancart oguzhan18/