fars
An unofficial Rust client for the Firebase Auth REST API.
Installation
Please install this library by adding by CLI:
$ cargo add fars
or adding dependency to your Cargo.toml:
[]
= "0.2.0"
Features
All features in this crate are as follows:
- default
- (Optional)
verify - (Optional)
custom_client
Supported APIs
Suppoted APIs of the Firebase Auth REST API are as follows:
- Exchange custom token for an ID and refresh token
- Exchange a refresh token for an ID token
- Sign up with email / password
- Sign in with email / password
- Sign in anonymously
- Sign in with OAuth credential
- Fetch providers for email
- Send password reset email
- (Not tested) Verify password reset code
- (Not tested) Confirm password reset
- Change email
- Change password
- Update profile
- Get user data
- Link with email/password
- Link with OAuth credential
- Unlink provider
- Send email verification
- (Not tested) Confirm email verification
- Delete account
[!NOTE] Unsupported APIs have already been implemented but not tested.
Supported OAuth ID providers
Supported OAuth ID provides are as follows:
- (Not implemented) Apple (
apple.com) - (Not implemented) Apple Game Center (
gc.apple.com) - (Not tested) Facebook (
facebook.com) - (Not implemented) GitHub (
github.com) - Google (
google.com) - (Not implemented) Google Play Games (
playgames.google.com) - (Not implemented) LinkedIn (
linkedin.com) - (Not implemented) Microsoft (
microsoft.com) - (Not tested) Twitter (
twitter.com) - (Not implemented) Yahoo (
yahoo.com)
[!NOTE] Unsupported providers have either not been tested or the format of
IdpPostBodyis not documented at the official API reference.
API Usages
Provides semantic interfaces based on a session (fars::Session) as following steps.
[!IMPORTANT]
- ID token (
fars::Session.id_token) has expiration date.- API calling through a session automatically refresh an ID token by the refresh token API when the ID token has been expired.
- All APIs through session cosume session and return new session that has same ID token or refreshed one except for the delete account API.
Therefore you have to update session every time you use APIs through a session by returned new session.
A usage for a siging in user
- Create a config (
fars::Config) with your Firebase project API key. - Sign in or sign up by supported options (Email & password / OAuth / Anonymous / Stored refresh token) through the config then get the session (
fars::Session) for the siging in user. - Use Auth APIs for the siging in user through the session, or use ID token (
fars::Session.id_token) for other Firebase APIs.
A sample code to sign up with email / password and to get user data with tokio and anyhow is as follows:
use Config;
use ApiKey;
use Email;
use Password;
async
A usage for not siging in user
- Create a config (
fars::Config) with your Firebase project API key. - Use Auth APIs for a not siging in user through the config.
A sample code to send password reset email with tokio and anyhow is as follows:
use Config;
use ApiKey;
use Email;
async
Sign in with OAuth credentials
[!IMPORTANT] This crate does not provide methods to get OAuth credential for each ID provider.
When you use signing in with OAuth credential, please implement a method to get target OAuth credential.
See also supported OAuth providers.
Google OAuth
To sign in with Google OAuth credential,
- Create a config (
fars::Config) with your Firebase project API key. - Get OpenID token from Google OAuth API. See reference.
- Sign in with specifying
request_uriandIdpPostBody::Google.
A sample code to sign in with Google OAuth credential with tokio and anyhow is as follows:
use Config;
use ApiKey;
use OAuthRequestUri;
use IdpPostBody;
async
Error handling
If you handle error in this crate, please handle fars::Result and fars::Error.
[!NOTE]
fars::Error::ApiErrorhas an error code (fars::error::CommonErrorCode) according to common error codes in the API reference. You can specify error type of an API error of Firebase Auth by matching an error code (fars::error::CommonErrorCode).
A sample code to handle error for signing in with email / password with reqwest, tokio and anyhow is as follows:
use Config;
use ApiKey;
use Email;
use Password;
async ,
// Handle internal errors
| _ => ,
}
},
}
}
Raw API interfaces
Provides raw supported APIs by fars::api module.
A sample code to sign in with email / password with reqwest, tokio and anyhow is as follows:
use ApiKey;
use Client;
use api;
async
(Optional) ID token verification
Provides ID token verification of the Firebase Auth via fars::verification module.
[!NOTE] ID token verification is an optional feature.
Please activate this feature by CLI:
$ cargo add fars --features verifyor adding features to your
Cargo.toml:[] = { = "0.2.0", = ["verify"] }
A sample code to verify ID token with tokio and anyhow is as follows:
use VerificationConfig;
use ProjectId;
use IdToken;
async
HTTP client customization
Provides HTTP client customization interface for Firebase Auth APIs.
[!NOTE] HTTP client customization is an optional feature.
Please activate this feature by CLI:
$ cargo add fars --features custom_clientor adding features to your
Cargo.toml:[] = { = "0.2.0", = ["custom_client"] }
An example to customize timeout options of HTTP client with tokio and anyhow is as follows:
use Duration;
use Client;
use ApiKey;
use Config;
async
Other examples
Please refer /examples directory, a shell script and a study of authentication on Web frontend with dioxus.
Changelog
See CHANGELOG.
License
Licensed under either of the Apache License, Version 2.0 or the MIT license at your option.