rocket-firebase-auth
Firebase Auth with Rocket, batteries included
- Tiny:
rocket-firebase-auth
is tiny, with features allowing you to make it even tinier - Does one thing well: Encodes/decodes Firebase JWT tokens in Rocket apps, and that's it
Upgrading from v2 to v3
1. Switching to the builder pattern
In v3, by following Rust's builder pattern, we have a more fluent client builder.
try_from_env_with_filename
=> env_file
// v2 try_from_env_with_filename
let firebase_auth = try_from_env_with_filename
.unwrap;
// v3 env_file
let firebase_auth = builder
.env_file
.build
.unwrap;
try_from_env
=> env
// v2 try_from_env
let firebase_auth = try_from_env
.unwrap;
// v3 env
let firebase_auth = builder
.env
.build
.unwrap;
try_from_json_file
=> json_file
// v2 try_from_json_file
let firebase_auth = try_from_json_file
.unwrap;
// v3 json_file
let firebase_auth = builder
.json_file
.build
.unwrap;
2. Changes to imports
We can change the imports for commonly used structs as follows
// v2
use ;
// v3
use
Getting started
1. Set Firebase service account keys as env variables
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.
Don't forget to add the firebase-credentials.json
file to your .gitignore
.
# Firebase service account's secret credentials
firebase-credentials.json
2. Create a FirebaseAuth
instance and add to server state
Add rocket-firebase-auth
to your project.
= "0.3.0"
Now, you can create a FirebaseAuth
struct by reading the json file with a helper
function included with the default import.
use ;
use ;
async
3. Verify the token from the endpoint function
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
.
use ;
use ;
// 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
async
Example project
For a more detailed example with a frontend example as well, checkout the example projects .
Testing
To run tests, run the following command:
Contribute
Any contributions (PRs, Issues) are welcomed!
Questions
If you have any questions, however trivial it may seem, please let me know via Issues. I will respond!
License
MIT