Home › Projects › User Auth API
API

User Auth API

A small Rust service that owns user accounts and watch progress: bcrypt-hashed passwords, 24-hour JWTs, and a MongoDB store. Public traffic reaches it through the Mecca API.

Rust 2024axum 0.7TokioMongoDBbcryptjsonwebtoken

Overview

This service is the owner of two collections: users and watch progress. It is not exposed to the internet directly — the Mecca API gateway forwards /api/auth/* and /api/progress to it.

Gateway routeService route
POST /api/auth/registerPOST /users/register
POST /api/auth/loginPOST /users/login
POST|GET /api/progressPOST|GET /progress

Endpoints

Users

POST/users/register

Create a user. The password is hashed with bcrypt before it is stored.

Request
{
  "username": "gaurav",
  "email": "[email protected]",
  "password": "••••••••"
}
Response
201 Created
{ "message": "user created", "id": "..." }
StatusBody
400{"error": "username required"} (or email / password)
409{"error": "username already exists"}
POST/users/login

Verify credentials and issue a JWT. The token's sub is the username and it expires after 24 hours.

Request
{
  "username": "gaurav",
  "password": "••••••••"
}
Response
200 OK
{ "token": "eyJhbGciOi..." }
StatusBody
401{"error": "invalid credentials"}

Watch progress

Every progress route needs Authorization: Bearer <token>; the user comes from the token, never from the body.

POST/progressBearer token

Upsert the position for one episode. If finished is sent it wins; otherwise the episode counts as finished once the position is close to the end.

Request
{
  "show": "One Piece",
  "path": "Episode 01.mkv",
  "position": 612.4,
  "duration": 1420.0,
  "finished": false
}
StatusBody
400{"error": "show and path required"}
401{"error": "missing bearer token"} / invalid or expired token
GET/progressBearer token

Latest watched episode per show.

GET/progress?show={show}Bearer token

All episodes watched in one show, newest first.

Running it

cargo build --release
PORT=... MONGODB_URI=... JWT_SECRET=... \
  ./target/release/user_auth_api

JWT_SECRET is required — the service refuses to start without it. CORS allows any origin.