Firebase Auth with Rocket, batteries included
rocket-firebase-auth
is tiny, with features allowing you to make it even tinierIn v3, by following Rust's builder pattern, we have a more fluent client builder.
try_from_env_with_filename
=> env_file
rust
// v2 try_from_env_with_filename
let firebase_auth = FirebaseAuth::try_from_env_with_filename(
".env.test",
"FIREBASE_CREDS",
)
.unwrap();
rust
// v3 env_file
let firebase_auth = FirebaseAuth::builder()
.env_file(
".env.test",
"FIREBASE_CREDS",
)
.build()
.unwrap();
try_from_env
=> env
rust
// v2 try_from_env
let firebase_auth = FirebaseAuth::try_from_env(
"FIREBASE_CREDS",
)
.unwrap();
rust
// v3 env
let firebase_auth = FirebaseAuth::builder()
.env("FIREBASE_CREDS")
.build()
.unwrap();
try_from_json_file
=> json_file
rust
// v2 try_from_json_file
let firebase_auth = FirebaseAuth::try_from_json_file("tests/env_files/firebase-creds.json")
.unwrap();
rust
// v3 json_file
let firebase_auth = FirebaseAuth::builder()
.json_file("firebase-creds.json")
.build()
.unwrap();
We can change the imports for commonly used structs as follows
rust
// v2
use rocket_firebase_auth::{
auth::FirebaseAuth
bearer_token::BearerToken
};
rust
// v3
use rocket_firebase_auth::{
FirebaseAuth,
BearerToken
}
If you haven't already, create a service account in Firebase for the Rocket backend
you are creating. Generate a new private key and copy-paste the generated json
into a firebase-credentials.json
file.
json
{
"type": "*********",
"project_id": "***********",
"private_key_id": "*************",
"private_key": "*****************",
"client_email": "*********",
"client_id": "*******",
"auth_uri": "********",
"token_uri": "********",
"auth_provider_x509_cert_url": "********",
"client_x509_cert_url": "********"
}
Don't forget to add the firebase-credentials.json
file to your .gitignore
.
```gitignore
firebase-credentials.json ```
FirebaseAuth
instance and add to server stateAdd rocket-firebase-auth
to your project.
toml
rocket_firebase_auth = "0.3.0"
Now, you can create a FirebaseAuth
struct by reading the json file with a helper
function included with the default import.
```rust use rocket::{routes, Build, Rocket}; use rocketfirebaseauth::{ FirebaseAuth };
struct ServerState { auth: FirebaseAuth }
async fn rocket() -> Rocket
rocket::build()
.mount("/", routes![hello_world])
.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 use rocket::{get, http::Status, routes, Build, Rocket, State}; use rocketfirebaseauth::{BearerToken, FirebaseAuth};
struct ServerState { auth: FirebaseAuth, }
// Example function that returns an Ok
and prints the verified user's uid.
// If the token is invalid, return with a Forbidden
status code.
async fn hello_world(state: &State
match token // extract uid from decoded token
{
Ok(token) => {
println!("Authentication succeeded with uid={}", token.uid);
Status::Ok
}
Err(_) => {
println!("Authentication failed.");
Status::Forbidden
}
}
}
async fn rocket() -> Rocket
rocket::build()
.mount("/", routes![hello_world])
.manage(ServerState {
auth: firebase_auth,
})
} ```
For a more detailed example with a frontend example as well, checkout the example projects .
To run tests, run the following command:
bash
cargo test -- --test-threads=1
Any contributions (PRs, Issues) are welcomed!
If you have any questions, however trivial it may seem, please let me know via Issues. I will respond!
MIT