From 34c9470799b28a9e6908996f18c52f3062899706 Mon Sep 17 00:00:00 2001 From: Eze Sunday Date: Wed, 29 Nov 2023 05:39:25 +0100 Subject: [PATCH] add example to readme --- readme.md | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/readme.md b/readme.md index 0767a1f..3060d0c 100644 --- a/readme.md +++ b/readme.md @@ -127,6 +127,54 @@ let app = Router::new() })); ``` +A more wholistic example: + +```rs +use crate::{ + auth, + service::{hello, MyUserData}, +}; +use axum::{ + middleware, + routing::{get, post}, + Json, Router, +}; + +use chrono::{Duration, Utc}; + + +pub fn create_router() -> Router { + let user_data = MyUserData; + let jwt_secret = "secret"; + + let app = Router::new() + .route( + "/hello", + get(hello) + .layer(middleware::from_fn(move |req, next| { + let key = auth::DecodingKey::from_secret(jwt_secret.as_ref()); + let validation = auth::Validation::default(); + async move { auth::verify_user(req, &key, validation, next).await } + })), + ) + .route( + "/login", + post(move |body: Json| { + let expiry_timestamp = Utc::now() + Duration::hours(48); + + auth::login( + body, + user_data.clone(), + jwt_secret, + expiry_timestamp.timestamp(), + ) + }), + ); + app +} + +``` +

You're all set!

## Features