A lightweight, session‑based authentication backend for web chat built with Rocket, SQLx, Argon2, and WebSockets.
- User registration with Argon2 password hashing
- Login logout endpoints
- Session‑based authentication using secure cookies
- Database integration via SQLx
- WebSocket chat support
- /me endpoint to verify sessions
- Clean service‑layer architecture
- Rust
- Rocket
- SQLx
- Argon2
- Serde
- MySql
- WebSockets
SRC
│ db.rs
│ main.rs
│
├───models
│ auth.rs
│ chat_message.rs
│ datetime.rs
│ mod.rs
│
└───routes
│ auth.rs
│ chat.rs
│ history.rs
│ mod.rs
│
└───services
auth.rs
chat.rs
mod.rs
To connect a database to rocket, you have to create Rocket.toml
and specify address, port, URL and database name.
[default]
address = "127.0.0.1"
port = 8000
[default.databases.mysql_logs]
url = "mysql://USER:PASSWORD@localhost:3306/DB_NAME"For full configuration, see the official guide: https://rocket.rs/guide/v0.5/state/#databases
[dependencies]
rocket = {version = "0.5.1", features = ["json"] }
tokio = { version = "1", features = ["full"] }
sqlx = { version = "0.7.4", features = ["mysql", "runtime-tokio", "macros", "time", "chrono"] }
rocket_ws = "0.1.1"
chrono = "0.4.45"
argon2 = "0.5"
password-hash = "0.5"
rand_core = { version = "0.6", features = ["std"] }
[dependencies.rocket_db_pools]
version = "0.2.0"
features = ["sqlx_mysql"]It is important to use SQLx with version 0.7.4 and "chrono" feature enabled
so the system can properly encode 'DATETIME' from database
- Handles user registration. Creates a session and user and saves it to a database
- Handles login requests. Creates a session and saves it to a database
- Deletes session and removes cookie
- Confirms the user, returns username and color
- Returns the message history
- Upgrades to WebSocket connection
First I watched an interesting guide from 'Let’s Get Rusty' channel and decided
to re-implement it from scratch. After I did that, I understood that
this project is missing database persistence, a proper WebSocket connection
and other fancy things like cookies and users, so I implemented them.
Also I have to say that there is an issue with .js file, but according to me
not having knowlege of .js I let it stay that messy way
I rate it 8/10 rusty tubes.
This project is licensed under the MIT License.
Copyright (c) 2026 LeakyDutchmann
See the LICENSE file for full details.