This API provides endpoints for user registration and login using Actix web framework and MongoDB.
Registers a new user with the provided username and password.
- URL:
/register
- Method:
POST
- Request Body: JSON
Parameter | Type | Description |
---|---|---|
username |
string | User's username |
password |
string | User's password |
- 200 OK: User registered successfully
- 500 Internal Server Error: Failed to register user
Authenticates a user with the provided username and password.
- URL:
/login
- Method:
POST
- Request Body: JSON
Parameter | Type | Description |
---|---|---|
username |
string | User's username |
password |
string | User's password |
- 200 OK: Login successful
- 401 Unauthorized: Invalid credentials
- 500 Internal Server Error: Failed to login
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
andpassword
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.
- Make sure Rust and MongoDB are installed on your system.
- Run
cargo run
to start the API server. - Send POST requests to
/register
and/login
endpoints using JSON body to register and login users.
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, send an email to oguzhancart1@gmail.com or you are invited to our telegram channel https://t.me/vsform_tr