Skip to content

Commit

Permalink
add example to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
ezesundayeze committed Nov 29, 2023
1 parent d1919c6 commit 34c9470
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<auth::RequestBody>| {
let expiry_timestamp = Utc::now() + Duration::hours(48);

auth::login(
body,
user_data.clone(),
jwt_secret,
expiry_timestamp.timestamp(),
)
}),
);
app
}

```

<p>You're all set!</p>

## Features
Expand Down

0 comments on commit 34c9470

Please sign in to comment.