https://github.com/marirs/rocket-jwt
Rocket api server with JWT
https://github.com/marirs/rocket-jwt
jwt jwt-auth jwt-authentication rocket rust rust-lang
Last synced: about 1 year ago
JSON representation
Rocket api server with JWT
- Host: GitHub
- URL: https://github.com/marirs/rocket-jwt
- Owner: marirs
- License: mit
- Created: 2021-06-22T02:12:10.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-05-31T03:54:27.000Z (about 2 years ago)
- Last Synced: 2025-04-18T08:39:36.676Z (about 1 year ago)
- Topics: jwt, jwt-auth, jwt-authentication, rocket, rust, rust-lang
- Language: Rust
- Homepage:
- Size: 56.6 KB
- Stars: 2
- Watchers: 1
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rocket-jwt
[](https://github.com/marirs/rocket-jwt/actions/workflows/ci.yml)
[](https://travis-ci.com/marirs/rocket-jwt)

A barebones [Rocket](https://rocket.rs) API with [JWT](https://jwt.io) authentication and
database integration.
The database integration can be switched between [sqlite](https://www.sqlite.org/index.html) and [postgres](https://www.postgresql.org/) with a simple feature flag.
## Requirement
- Rust version 1.52 or newer.
- Diesel CLI with `postgres` or `sqlite` features.
- A running [PostgreSQL](https://www.postgresql.org/) or [Sqlite](https://www.sqlite.org/index.html) backend.
## Setup environment
A `.env.{db}` at the root directory exposes environment both used by `diesel`and the project itself.
Rename it to `.env` then set all the environment variables before running the following commands :
``` bash
source .env
```
## Build locally
Run the following command to fulfill the requirements :
``` bash
# Install Rust and cargo alongside rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
# Install ORM and query builder
cargo install diesel_cli --no-default-features --features "postgres sqlite"
# Run migrations
diesel setup
diesel migration run
```
Then build the project with `default` database `(sqlite)`:
```bash
cargo build --release
```
or build with `postgresql` database:
``` bash
$ cargo build --release --no-default-features --features postgres
```
## Create the first user
```bash
# Build the create-user binary
cargo build --release --features create-user-binary
# Execute the binary to create the first admin user
../target/release/create-user
```
## Available `feature` flags
You can use the feature flag to switch between database use:
- sqlite (default)
- postgres
To build the create-user binary
- create-user-binary
## Switching the database at the backend
Any time you want to perform a database switch, you have to:
1. Install and run the desired database on your machine if is not already the case
2. Run migrations
3. Build the project with the respective feature flag.
## Usage
To print the project usage, an option `-h` is available.
## API routes
- Get Swagger docs
``` http
GET /docs
```
- Create a new user
```http
POST /users
Authorization: Bearer
{
"username": "string",
"password": "string",
"email": "string",
"is_admin": boolean
}
```
- Authenticate
```http
POST /auth
{
"username": "string",
"password": "string"
}
```
- Get a list of users
```http
GET /users
Authorization: Bearer
```
- Delete an existing user
```http
DELETE /users/
Authorization: Bearer
```
---
License: MIT