Repository navigation
Expand file tree
/
Copy pathaxum.rs
More file actions
40 lines (35 loc) · 1.39 KB
/
Copy pathaxum.rs
File metadata and controls
40 lines (35 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use axum::{routing::get, Extension, Router};
use clerk_rs::{
clerk::Clerk,
validators::{authorizer::ClerkJwt, axum::ClerkLayer, jwks::MemoryCacheJwksProvider},
ClerkConfiguration,
};
/// This is an unprotected route.
async fn index() -> &'static str {
"Hello world! This is a public route that requires no authentication."
}
/// This is a protected route. You can grab the currently authed user's
/// ClerkJwt using an Extension extractor like so. Make sure you only
/// use this extractor on protected routes, or else you will get a
/// runtime error.
async fn profile(Extension(clerk_jwt): Extension<ClerkJwt>) -> String {
format!("Hello, {}! This is an example of a protected route.", clerk_jwt.sub)
}
#[tokio::main]
async fn main() -> std::io::Result<()> {
let config = ClerkConfiguration::new("your_secret_key");
let clerk = Clerk::new(config);
let app = Router::new()
.route("/", get(index))
.route("/index", get(index))
.route("/profile", get(profile))
.layer(ClerkLayer::new(
MemoryCacheJwksProvider::new(clerk),
Some(vec![String::from("/profile")]),
true,
));
// ^^ The second argument of the ClerkLayer constructor is for flagging which routes should be protected
// by the middleware and which should not be protected. If None is provided, all routes are protected
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080").await?;
axum::serve(listener, app).await
}