#wasm-bindings #firebase #bindings

firebase-js-rs

非官方 Firebase JS SDK 的 Wasm 绑定

2 个版本

0.1.1 2023 年 2 月 27 日
0.1.0 2023 年 2 月 26 日

WebAssembly 类别中排名第 1197

每月下载量 40

MIT/Apache

7KB
70

firebase-js-rs

使用 Rust 编写的 Firebase JS SDK 的非官方 Wasm 绑定。

开始使用

以下示例演示了如何将电子邮件和密码登录添加到 Sycamore 应用程序。

添加和初始化 SDK

在您的项目目录中运行以下 Cargo 命令以安装 firebase-js-rs

cargo add firebase-js-rs

或者,将以下行添加到您的 Cargo.toml 中

firebase-js-rs = "0.1.1"

然后在 Firebase 控制台 中添加项目,并从 CDN 安装 JS SDK。

<html>
  <head>
    <script src="https://www.gstatic.com/firebasejs/9.17.1/firebase-app-compat.js"></script>
    <script src="https://www.gstatic.com/firebasejs/9.17.1/firebase-auth-compat.js"></script>
  </head>
</html>

初始化 Firebase 应用程序并获取认证服务的引用

use sycamore::prelude::*;
use firebase_js_rs::{app::initialize_app, Config};

fn main() {
  sycamore::render(|cx| {
    view! { cx,
          // Initialize Firebase
          let firebase_app = initialize_app(Config::initialize(
            "api_key", None, None, None, None, None, None,
            ));
          // Get reference to the auth service
            let auth = app.auth();
        }
    });
}

创建新用户

let result = auth.create_user_with_email_and_password(email, password).await;

用户登录

let result = auth.sign_in_with_email_and_password(email, password).await;

观察认证状态

let callback = Closure::new(move |user: JsValue| {
  // Get info about user
});
auth.on_auth_state_changed(&callback);
callback.forget();

依赖

~1.1–3MB
~59K SLoC