Encode/decode Firebase tokens in Rocket apps with ease.
If you haven't already, create a service account for the Rocket server you are
adding firebase to. Generate a new private key and copy paste the generated json
into a .env
file.
dotenv
FIREBASE_ADMIN_CERTS='{ "type": "service_account", ... }'
FirebaseAuth
instance by reading the env variableYou can create a FirebaseAuth
struct by deserializing the env string that we set
into FirebaseAdmin
struct and call the FirebaseAuth::with_firebase_admin()
function.
rust
dotenv().ok();
let firebase_admin_certs = env::var("FIREBASE_ADMIN_CERTS").unwrap();
let firebase_admin = serde_json::from_str::<FirebaseAdmin>( & firebase_admin_certs)
.unwrap();
let firebase_auth = FirebaseAuth::with_firebase_admin(firebase_admin);
FirebaseAuth
to the managed server state in RocketIn order to access the FirebaseAuth
instance from our endpoint functions, add
it to the server state.
```rust pub struct ServerState { pub auth: FirebaseAuth }
async fn rocket() -> Rocket
rocket::build()
.mount("/", routes![...])
.manage(ServerState {
auth: firebase_auth
})
} ```
On endpoints that we except to receive Authorization headers containing our encoded
Firebase tokens from the client, we can add a field to the endpoint function.
Running the Jwt::verify()
function will decode the token, where you can get the
Firebase uid
.
```rust
async fn helloworld(
state: &State
status::Accepted(Some(format!("uid: {uid}")))
} ```
For a more detailed example with a frontend example as well, checkout the example projects .
Any contributions (PRs, Issues) are welcomed!
MIT