Crates.io | rocket_firebase_auth |
lib.rs | rocket_firebase_auth |
version | 0.5.0 |
source | src |
created_at | 2022-10-29 07:23:48.185186 |
updated_at | 2024-03-11 15:11:19.616529 |
description | Encode/decode firebase tokens in rocket apps with ease |
homepage | |
repository | https://github.com/DrPoppyseed/rocket-firebase-auth |
max_upload_size | |
id | 700886 |
size | 33,736 |
Firebase Auth with Rocket, batteries included
rocket-firebase-auth
is tiny, with features allowing you to make it even tinierIf 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. It should look something like the json snippet below.
{
"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
.
# Firebase service account's secret credentials
firebase-credentials.json
FirebaseAuth
and get startedAdd rocket-firebase-auth
to your project.
rocket_firebase_auth = "0.5"
Now, you can create a FirebaseAuth
struct by reading the json file with a helper
function included with the default import.
use rocket::{get, http::Status, routes, Build, Rocket};
use rocket_firebase_auth::{FirebaseAuth, FirebaseToken};
// Setup the server state, which will include your FirebaseAuth instance, among
// other things like the connection pool to your database.
struct ServerState {
auth: FirebaseAuth,
}
#[get("/")]
async fn hello_world(token: FirebaseToken) -> Status {
println!("Authentication succeeded with uid={}", token.sub);
Status::Ok
}
#[rocket::launch]
async fn rocket() -> Rocket<Build> {
let firebase_auth = FirebaseAuth::builder()
.json_file("firebase-credentials.json")
.build()
.unwrap();
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:
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