#firebase #jwt #password #email

firebase-user

为Rust提供的Firebase认证

1 个不稳定版本

0.2.0 2022年11月4日

#406认证

MIT 许可证

68KB
368 代码行

Fire Auth

Rust对Firebase Authentication REST API的封装

WTFPL WTFPL

安装

将以下内容添加到Cargo.toml中

firebase-user = "0.2"

使用方法

首先,您需要从Firebase项目设置获取一个Web API_KEY

let api_key: String = String::from("s6FqaFcRFd...njhB8cCjN7");

let auth = firebase_user::FireAuth::new(API_KEY);

功能

  1. 注册(电子邮件)
  2. 登录(电子邮件)
  3. 发送OOB代码
  4. 刷新ID令牌
  5. 获取用户信息
  6. 更新电子邮件和密码

没有您需要的功能?请查看下面的当前不支持的功能


1. 注册(电子邮件)

let email = "[email protected]";
let password = "supersecret";
let return_secure_token = true;

match auth.sign_up_email(email, password, return_secure_token).await {
    Ok(response) => ...,
    Err(error) => ...,
}

// response structure
pub struct firebase_user::api::SignUpResponse {
    pub id_token: String,
    pub email: String,
    pub refresh_token: String,
    pub expires_in: String,
    pub local_id: String,
}

2. 登录(电子邮件)

match auth.sign_in_email(email, password, return_secure_token).await {
    Ok(response) => ...,
    Err(error) => ...,
}

// response structure
pub struct firebase_user::api::SignInResponse {
    pub kind: String,
    pub local_id: String,
    pub email: String,
    pub display_name: String,
    pub id_token: String,
    pub registered: bool,
    pub refresh_token: Option<String>,
    pub expires_in: Option<String>,
}

3. 发送OOB代码

发送验证电子邮件

match auth.verify_email(id_token).await {
    Ok(send_oob_code) => ...
    Err(error) => ...
}

// response structure
pub struct firebase_user::api::SendOobCode {
    pub kind: String,
    pub email: String,
}

发送重置密码

match auth.reset_password(email).await {
    Ok(send_oob_code) => ...
    Err(error) => ...
}

4. 刷新ID令牌

match auth.refresh_id_token(refresh_token).await {
    Ok(refresh_id_token_response) => ...
    Err(error) => ...
}

// response structure
pub struct firebase_user::api::RefreshIdToken {
    pub access_token: String,
    pub expires_in: String,
    pub token_type: String,
    pub refresh_token: String,
    pub id_token: String,
    pub user_id: String,
    pub project_id: String,
}

5. 获取用户信息

match auth.get_user_info(id_token).await {
    Ok(user) => ...,
    Err(error) => ...,
}

// response structure
pub struct firebase_user::api::User {
    pub local_id: String,
    pub email: String,
    pub password_hash: String,
    pub email_verified: bool,
    pub password_updated_at: u64,
    pub provider_user_info: Vec<ProviderUserInfo>,
    pub valid_since: String,
    pub last_login_at: String,
    pub created_at: String,
    pub last_refresh_at: String,
}

pub struct firebase_user::api::ProviderUserInfo {
    pub provider_id: String,
    pub federated_id: String,
    pub email: String,
    pub raw_id: String,
}

6. 更新电子邮件和密码

电子邮件

match auth.change_email(id_token, email, return_secure_token).await {
    Ok(update_user) => ...
    Err(error) => ...
}

// response structure
pub struct firebase_user::api::UpdateUser {
    pub kind: String,
    pub local_id: String,
    pub email: String,
    pub provider_user_info: Vec<ProviderUserInfo>,
    pub password_hash: String,
    pub email_verified: bool,
    pub id_token: Option<String>,
    pub refresh_token: Option<String>,
    pub expires_in: Option<String>,
}

pub struct firebase_user::api::ProviderUserInfo {
    pub provider_id: String,
    pub federated_id: String,
    pub email: String,
    pub raw_id: String,
}

密码

match auth.change_password(id_token, password, return_secure_token).await {
    Ok(update_user) => ...
    Err(error) => ...
}

当前不支持的功能

登录

  • 匿名登录
  • 使用OAuth凭证登录

密码

  • 验证密码重置码
  • 确认密码重置

用户

  • 更新个人资料
  • 删除账户

依赖项

~3–19MB
~244K SLoC