A simple and small Rust Actix web framework Extractor for verifing JWT token from Firebase Authentication.
Dependencies:
toml
[dependencies]
actix-web = "4"
Code:
```rust use actixweb::{get, middleware::Logger, web::Data, App, HttpServer, Responder}; use envlogger::Env; use firebase_auth::{FirebaseAuth, FirebaseUser};
// Use FirebaseUser
extractor to verify the user token and decode the claims
async fn greet(user: FirebaseUser) -> impl Responder { format!("Hello {}!", user.email) }
async fn main() -> std::io::Result<()> { envlogger::initfromenv(Env::default().defaultfilter_or("debug"));
// create Application State for the `FirebaseAuth` it will refresh the public keys
// automatically.
// Change project_id to your Firebase [Project ID](https://firebase.google.com/docs/projects/learn-more#project-id)
let firebase_auth = tokio::task::spawn_blocking(|| FirebaseAuth::new("my-project-id"))
.await
.expect("panic init FirebaseAuth");
let app_data = Data::new(firebase_auth);
HttpServer::new(move || {
App::new()
.wrap(Logger::default())
.app_data(app_data.clone())
.service(greet)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
} ```
Copyright (c) 2022-, Quang Tran.