axum_jwt_ware Integration Guide
Simple Axum + JWT authentication middleware with implemented Login and refresh token.
Goal
I aim to simplify the process for developers/indie hackers to focus on writing their core business logic when starting a new project, rather than spending time rewriting authentication.
Installation
Usage
1. Implement the UserData trait
You need to create a struct and implement the UserData trait for it. This trait has two methods: get_user_by_email and verify_password. These methods are responsible for fetching a user from your database and verifying their password.
use ;
use async_trait;
;
2. Create the Authenticator
The Authenticator struct holds all the necessary configuration, such as the keys, validation, and user data implementation.
use ;
let user_data = MyUserData;
let jwt_key = from_secret;
let refresh_key = from_secret;
let jwt_decoding_key = from_secret;
let refresh_decoding_key = from_secret;
let validation = default;
let auth = new;
3. Protect your routes
You can use the Authenticator to protect your routes by calling the layer() method.
use ;
async
let app = new
.route
.layer;
Any route under the auth_layer will have access to the Claims from the JWT in the request extensions. You can extract it like this:
use ;
use Claims;
async
4. Implement the login handler
The Authenticator provides a login method that you can use to implement your login handler.
use ;
use RequestBody;
let app = new.route;
5. Implement the refresh token handler
The Authenticator provides a refresh method that you can use to implement your refresh token handler.
use ;
use ;
use ;
let app = new.route;
Example
You can find a working example in the examples/helloworld directory in the GitHub Repo.
Features
- Refresh Token
- Login
- Authentication Middleware
- Tests
Want to contribute?
- Create an issue
- Fork the repo
- Create a PR that fixes the issue