Skip to content

Stipulations/actix-jwt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 

Repository files navigation

JWT Authentication with Actix-Web and Rusqlite

This is a simple web application built with Rust's Actix-Web framework and the Rusqlite database. It demonstrates how to implement JWT (JSON Web Token) authentication for user registration, login, and access to protected routes.

Features

  • User registration with username and password
  • User login with JWT token generation
  • Protected route accessible only with a valid JWT token
  • Unprotected route accessible without authentication

ToDo

  • Password Hashing:
    Switch to using bcrypt for hashing passwords. Update the registration and login processes to work with hashed passwords.

  • Secret Key Management:
    Move the JWT secret key to a .env file and adjust the app to pull it in securely.

  • Async DB Operations:
    Use tokio::task::spawn_blocking to handle SQLite operations without blocking.

  • Role-Based Access:
    Set up roles like Admin and User, store them in the database, and make sure the JWT includes role info for access control.

  • Error Handling:
    Clean up error messages to make them clearer for users and more useful for debugging.

Getting Started

Prerequisites

  • Rust (latest stable version)
  • Cargo (Rust's package manager)

Installation

  1. Clone the repository:
git clone https://github.com/Stipulations/actix-jwt.git
  1. Navigate to the project directory:
cd actix-jwt
  1. Build the project:
cargo build

Running the Application

To run the application, use the following command:

cargo run

The application will start running on http://127.0.0.1:8080.

Usage

Register a New User

Send a POST request to http://127.0.0.1:8080/register with the following JSON payload:

{
  "username": "your_username",
  "password": "your_password"
}

Login

Send a POST request to http://127.0.0.1:8080/login with the same JSON payload as above. If the credentials are valid, you will receive a JWT token in the response.

Access Protected Route

To access the protected route at http://127.0.0.1:8080/protected, include the JWT token in the jwt header of your request.

Access Unprotected Route

The unprotected route at http://127.0.0.1:8080/unprotected can be accessed without any authentication.

Contributing

Contributions are welcome! If you find any issues or have suggestions for improvement, please open an issue or submit a pull request.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages