Giter Club home page Giter Club logo

firebase-auth's Introduction

Firebase Auth

A simple and small Rust library for handling Firebase Authorization.

Supports the two most popular frameworks: Tokio's Axum and Actix-web.

Build badge crates.io badge

Notice

Version 0.4.x supports Axum 0.7.

Version 0.3.x will continue to provide support and fix bugs for Axum 0.6.

Setup

Actix

[dependencies]
firebase-auth = { version = "<version>", features = ["actix-web"] }
actix-web = "4"

Axum

[dependencies]
firebase-auth = { version = "<version>", features = ["axum"] }
axum = "0.7"

Examples

Actix

https://github.com/trchopan/firebase-auth/tree/main/examples/actix_basic.rs

use actix_web::{get, middleware::Logger, web::Data, App, HttpServer, Responder};
use firebase_auth::{FirebaseAuth, FirebaseUser};

// Use `FirebaseUser` extractor to verify the user token and decode the claims
#[get("/hello")]
async fn greet(user: FirebaseUser) -> impl Responder {
    let email = user.email.unwrap_or("empty email".to_string());
    format!("Hello {}!", email)
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    // TODO: Change to your firebase project id
    let firebase_auth = FirebaseAuth::new("my-project-id").await;

    let app_data = Data::new(firebase_auth);

    HttpServer::new(move || {
        App::new()
            .wrap(Logger::default())
            .app_data(app_data.clone())
            .service(greet)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

Axum

https://github.com/trchopan/firebase-auth/tree/main/examples/axum_basic.rs

use axum::{routing::get, Router};
use firebase_auth::{FirebaseAuth, FirebaseAuthState, FirebaseUser};

async fn greet(current_user: FirebaseUser) -> String {
    let email = current_user.email.unwrap_or("empty email".to_string());
    format!("hello {}", email)
}

async fn public() -> &'static str {
    "ok"
}

#[tokio::main]
async fn main() {
    // TODO: Change to your firebase project id
    let firebase_auth = FirebaseAuth::new("my-project-id").await;

    let app = Router::new()
        .route("/hello", get(greet))
        .route("/", get(public))
        .with_state(FirebaseAuthState { firebase_auth });

    let addr = "127.0.0.1:8080";
    let listener = tokio::net::TcpListener::bind(addr).await.unwrap();

    axum::serve(listener, app).await.unwrap();
}

More complete example with Axum, SQLite and slqx

examples/axum-sqlite

This is more real world application with Firebase Authentication and SQLite as database.

Using Custom Claims

examples/actix-web-custom-claims

examples/axum-custom-claims

Custom claims are provided as defined FirebaseUser struct and use actix or axum trait to implement the extraction from the request.

How to call the endpoint with Bearer Token

Obtain the Bearer token

Use firebase sdk to get the User Token.

For example: getIdToken()

Request the endpoint with Authorization Bearer

Make the request using the User's token. Note that it will expire so you will need to get it again if expired.

TOKEN="<paste your token here>"

curl --header "Authorization: Bearer $TOKEN" http://127.0.0.1:8080/hello

Firebase Document

Verify ID tokens using a third-party JWT library

License

MIT

Copyright (c) 2022-, Quang Tran.

firebase-auth's People

Contributors

trchopan avatar a-pechenyi avatar armyofgnomes avatar wickwirew avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.