Firebase与Web的集成

Firebase与Web的集成

Firebase由谷歌在2014年创建,为其用户提供后端服务。它提供了各种高质量的服务,我们可以用来开发移动和Web应用程序。例如,它提供实时数据库、用户身份验证、云存储等。此外,它还提供分析工具,以分析应用程序的流量。由于它的快速设置,越来越受欢迎。

在本教程中,我们将学习如何将Firebase身份验证集成到单页Web应用程序中。

用户应按照以下步骤设置Firebase帐户并将其与单页Web应用程序集成。

  • 步骤 1 − 首先,访问 Firebase的网站 并创建一个帐户。

  • 步骤 2 − 现在,访问 https://console.firebase.google.com/u/0/ 打开Firebase控制台。

  • 步骤 3 − 现在,单击“创建项目”按钮开始创建新项目。

Firebase与Web的集成

  • 步骤 4 − 在此处填写所需的细节,并单击“继续”按钮。我们在这里创建一个“test”应用程序。

Firebase与Web的集成

  • 步骤 5 − 选择首选位置,接受条款和条件,并单击“创建项目”按钮。然后,请等待它为您创建项目。

Firebase与Web的集成

  • 步骤 6 − 它将重定向到下面的页面。在这里,单击“身份验证”卡片元素。之后,单击“开始使用”按钮。

Firebase与Web的集成

  • 步骤 7 − 转到“登录方法”选项卡,然后单击“电子邮件/密码”字段。之后,启用“电子邮件/密码”方法,并单击“保存”按钮。用户还可以在此处启用其他方法来对您的Web应用程序进行身份验证。

Firebase与Web的集成

  • 步骤 8 − 现在,单击“项目设置”并从那里获取API和项目ID。将其存储在某个地方。我们将在下面的示例中使用它。

Firebase与Web的集成

创建单页静态应用程序

现在,Firebase项目的设置完成了。接下来,我们将创建一个单页静态应用程序。

步骤

  • 步骤1 − 以任何一种方式向您的项目中添加Firebase。这里,我们使用了CDN添加。开发者还可以根据他们目前正在工作的项目使用SDK。

  • 步骤2 − 现在,建立一个简单的HTML模板,输入电子邮件和密码。还要添加注册、登录和注销按钮。

  • 步骤3 − 在JavaScript中,使用API密钥和项目ID初始化Firebase配置。

  • 步骤4 − 使用onAuthStateChanged()方法在身份验证状态更改时打印一条消息。

  • 步骤5 − 使用Firebase的auth()方法初始化身份验证。

  • 步骤6 − 现在,创建一个addUsers()函数以将用户添加到Firebase。在函数中访问电子邮件和密码,并使用createUserWithEmailAndPassword()方法将用户添加到Firebase。

  • 步骤7 − 现在,创建一个logIn()函数,并使用signInWithEmailAndPassword()方法使用电子邮件和密码登录应用程序。

  • 步骤8 − 还要创建一个logout()函数,该函数使用signOut()方法结束当前会话。

示例

在下面的示例中,我们创建了一个带有两个输入字段的简单表单。每当用户点击signUp按钮时,它调用addUsers()函数,该函数将用户添加到Firebase。如果用户输入的密码弱或电子邮件地址错误,Firebase会返回错误。

此外,当用户点击登录按钮时,它调用“login()”函数,该函数允许用户登录应用程序。如果用户输入错误的密码或电子邮件,Firebase会返回错误。当用户点击signOut按钮时,它执行signOut()函数,结束当前会话。

注意 :在这里,开发者需要根据他们的项目更改API密钥、项目ID和项目域。下面的凭据仅用于测试目的。

<html>
<head>
   <script src = "https://www.gstatic.com/firebasejs/8.2.7/firebase-app.js">
   </script>
   <script src = "https://www.gstatic.com/firebasejs/8.2.7/firebase-auth.js">
   </script>
   <style>
      button {
         width: 100px;
         height: auto;
         padding: 5px 10px;
         background-color: aqua;
         border: 2px solid green;
         border-radius: 12px;
      }
   </style>
</head>
<body>
   <h2>
      Using the <i> Firebase auth </i> to add authentication in a single page static website.
   </h2>
   <div class = "container">
      <h2>Enter the email and password below.</h2>
      <input type = "email" placeholder = "abcd@gamil.com" id = "email" />
      <br /> <br />
      <input type = "password" placeholder = "Add password" id = "password" />
      <br /> <br />
      <button onclick = "addUsers()" id = "signUp">
         SignUp
      </button>
      <button onclick = "login()" id = "logIp">
         SignIn
      </button>
      <button onclick = "logout()" id = "logOut">
         SignOut
      </button>
      <br> <br>
      <div id = "output"> </div>
   </div>
   <script>
      let output = document.getElementById('output');
      // Your web app's Firebase configuration
      var initialConfig = {
         apiKey: "AIzaSyBsYILuhF4wOGOe0rFhPudhVWO3cGh2z18", // change API keu
         authDomain: "localhost", // change domain
         projectId: "test-application-45005", // change project Id
      };

      // Initialize Firebase
      firebase.initializeApp(initialConfig);
      const authenticate = firebase.auth();

      // Check if there are any active users
      firebase.auth().onAuthStateChanged((user) => {
         if (user) {
            var email = user.email;
            output.innerHTML = "Active user is " + email + "<br>";
         } else {
            output.innerHTML = "No active users" + "<br>";
         }
      });

      // add users
      function addUsers() {
         var email = document.getElementById("email").value;
         var password = document.getElementById("password").value;

         // adding users via the promise
         authenticate.createUserWithEmailAndPassword(
            email,
            password
         ).then((userCredential) => {
            output.innerHTML = "User added successfully and user id is " + userCredential.user.uid + "<br>";
         }).catch((e) => {
            output.innerHTML = "Some error occurred - " + e.message + "<br>";
         });
      }

      // login function
      function login() {
         var email = document.getElementById("email").value;
         var password = document.getElementById("password").value;
         authenticate.signInWithEmailAndPassword(
         email, password).then((userCredential) => {
            output.innerHTML = "User login successfully and user id is " + userCredential.user.uid + "<br>";
         }).catch((e) => {
            output.innerHTML = "Some error occurred - " + e.message + "<br>";
         });
      }

      // logout currently logged-in user
      function logout() {
         authenticate.signOut();
         output.innerHTML = "User logout successfully";
      }

   </script>
</body>
</html>

用户学会了将Firebase与Web应用程序集成。对于有经验的开发者来说,将Firebase与任何Web应用程序集成几乎只需要15分钟。此外,如果用户在登录应用程序时输入了弱密码,它会给出一个错误,并且它会处理开发者不需要担心的其他所有事情。

另外,开发者可以将Firebase的数据库用于任何Web或移动应用程序。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程