跳至主要内容
在本页上

连接到 Firebase

Firebase 是 Google 开发的用于创建移动和 Web 应用程序的平台。其功能包括用于登录的身份验证原语和一个 NoSQL 数据存储 Firestore,您可以将数据持久化到其中。

本教程介绍如何从部署在 Deno Deploy 上的应用程序连接到 Firebase。

您可以在此处找到一个更全面的教程,该教程基于 Firebase 构建了一个示例应用程序。

从 Firebase 获取凭据 跳转到标题

本教程假设您已经在 Firebase 中创建了一个项目,并将一个 Web 应用程序添加到您的项目中。

  1. 在 Firebase 中导航到您的项目,然后点击项目设置

  2. 向下滚动,直到看到一个带有您的应用程序名称的卡片,以及一个包含 firebaseConfig 对象的代码示例。它应该类似于以下内容。请将其放在手边。我们稍后会用到它

    var firebaseConfig = {
      apiKey: "APIKEY",
      authDomain: "example-12345.firebaseapp.com",
      projectId: "example-12345",
      storageBucket: "example-12345.appspot.com",
      messagingSenderId: "1234567890",
      appId: "APPID",
    };
    

在 Deno Deploy 中创建项目 跳转到标题

  1. 转到 https://dash.deno.com/new(如果您尚未登录,请使用 GitHub 登录),然后点击从命令行部署下的+ 空项目

  2. 现在点击项目页面上的设置按钮。

  3. 导航到环境变量部分并添加以下内容

    FIREBASE_USERNAME
    上面添加的 Firebase 用户(电子邮件地址)。
    FIREBASE_PASSWORD
    上面添加的 Firebase 用户密码。
    FIREBASE_CONFIG
    Firebase 应用程序的配置,以 JSON 字符串形式提供。

    配置需要是有效的 JSON 字符串才能被应用程序读取。如果设置时给出的代码片段如下所示

    var firebaseConfig = {
      apiKey: "APIKEY",
      authDomain: "example-12345.firebaseapp.com",
      projectId: "example-12345",
      storageBucket: "example-12345.appspot.com",
      messagingSenderId: "1234567890",
      appId: "APPID",
    };
    

    您需要将字符串的值设置为这样(注意不需要空格和换行符)

    {
      "apiKey": "APIKEY",
      "authDomain": "example-12345.firebaseapp.com",
      "projectId": "example-12345",
      "storageBucket": "example-12345.appspot.com",
      "messagingSenderId": "1234567890",
      "appId": "APPID"
    }
    

编写连接到 Firebase 的代码 跳转到标题

我们要做的第一件事是导入 Firebase 在 Deploy 下工作所需的 XMLHttpRequest polyfill 以及 localStorage 的 polyfill,以允许 Firebase 身份验证保持用户登录状态。

import "https://deno.land/x/[email protected]/mod.ts";
import { installGlobals } from "https://deno.land/x/[email protected]/mod.ts";
installGlobals();

ℹ️ 我们使用的是编写本教程时的软件包的当前版本。它们可能不是最新的,您可能需要再次检查当前版本。

由于 Deploy 拥有许多 Web 标准 API,因此最好在 Deploy 下使用 Firebase 的 Web 库。目前 Firebase 的 v9 仍处于测试阶段,因此我们将使用 v8。

import firebase from "https://esm.sh/[email protected]/app";
import "https://esm.sh/[email protected]/auth";
import "https://esm.sh/[email protected]/firestore";

现在我们需要设置 Firebase 应用程序。我们将从之前设置的环境变量中获取配置,并获取我们将要使用的 Firebase 部分的引用。

const firebaseConfig = JSON.parse(Deno.env.get("FIREBASE_CONFIG"));
const firebaseApp = firebase.initializeApp(firebaseConfig, "example");
const auth = firebase.auth(firebaseApp);
const db = firebase.firestore(firebaseApp);

好了,我们快完成了。我们只需要创建我们的中间件应用程序并添加我们导入的 localStorage 中间件。

const app = new Application();
app.use(virtualStorage());

然后我们需要添加中间件来验证用户身份。在本教程中,我们只是从我们将要设置的环境变量中获取用户名和密码,但这可以很容易地调整为在用户未登录时将其重定向到登录页面。

app.use(async (ctx, next) => {
  const signedInUid = ctx.cookies.get("LOGGED_IN_UID");
  const signedInUser = signedInUid != null ? users.get(signedInUid) : undefined;
  if (!signedInUid || !signedInUser || !auth.currentUser) {
    const creds = await auth.signInWithEmailAndPassword(
      Deno.env.get("FIREBASE_USERNAME"),
      Deno.env.get("FIREBASE_PASSWORD"),
    );
    const { user } = creds;
    if (user) {
      users.set(user.uid, user);
      ctx.cookies.set("LOGGED_IN_UID", user.uid);
    } else if (signedInUser && signedInUid.uid !== auth.currentUser?.uid) {
      await auth.updateCurrentUser(signedInUser);
    }
  }
  return next();
});

将应用程序部署到 Deno Deploy 跳转到标题

完成应用程序编写后,您可以将其部署在 Deno Deploy 上。

为此,请返回到您的项目页面 https://dash.deno.com/projects/<project-name>

您应该会看到几个部署选项

  • Github 集成
  • deployctl
    deployctl deploy --project=<project-name> <application-file-name>
    

除非您要添加构建步骤,否则我们建议您选择 Github 集成。

有关在 Deno Deploy 上部署的不同方法以及不同配置选项的更多详细信息,请阅读此处