在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ 人工智能/ Firebase使用Google賬號(hào)登錄
Firebase數(shù)組
Firebase讀取數(shù)據(jù)
Firebase開(kāi)發(fā)環(huán)境設(shè)置
Firebase電子郵件認(rèn)證
Firebase事件類型
Firebase數(shù)據(jù)
Firebase寫入列表數(shù)據(jù)
Firebase連接檢查
Firebase簡(jiǎn)介
Firebase分離回調(diào)
Firebase查詢和排序
Firebase使用Google賬號(hào)登錄
Firebase使用Github賬號(hào)登錄
Firebase過(guò)濾數(shù)據(jù)
Firebase匿名身份驗(yàn)證
Firebase寫入事務(wù)數(shù)據(jù)
Firebase寫入數(shù)據(jù)
FireBase教程
Firebase安全

Firebase使用Google賬號(hào)登錄

在本章中,我們將介紹如何在Firebase中設(shè)置Google身份驗(yàn)證。

第1步 - 啟用Google身份驗(yàn)證
打開(kāi)Firebase信息中心,然后點(diǎn)擊左側(cè)菜單中的身份驗(yàn)證。 要打開(kāi)可用方法的列表,需要在標(biāo)簽菜單中單擊登錄方法。

現(xiàn)在可以從列表中選擇Google,啟用它并保存。參考下圖 -

第2步 - 創(chuàng)建登錄按鈕
創(chuàng)建一個(gè)文件:index.html,并將添加兩個(gè)按鈕。參考代碼如下 -

<button onclick = "googleSignin()">使用Google賬號(hào)登錄</button>
<button onclick = "googleSignout()">Google賬號(hào)注銷</button>

第3步 - 登錄和退出
在這一步中,我們將創(chuàng)建用于登錄和注銷的兩個(gè)函數(shù):googleSigningoogleSignout。 這里將使用signInWithPopup()signOut()函數(shù)。

示例

讓我們來(lái)看看下面的例子。參考函數(shù)的實(shí)現(xiàn) -

var provider = new firebase.auth.GoogleAuthProvider();

function googleSignin() {
   firebase.auth()

   .signInWithPopup(provider).then(function(result) {
      var token = result.credential.accessToken;
      var user = result.user;

      console.log(token)
      console.log(user)
   }).catch(function(error) {
      var errorCode = error.code;
      var errorMessage = error.message;

      console.log(error.code)
      console.log(error.message)
   });
}

function googleSignout() {
   firebase.auth().signOut()

   .then(function() {
      console.log('Signout Succesfull')
   }, function(error) {
      console.log('Signout Failed')  
   });
}

完整的代碼如下 -

<!DOCTYPE html>
<html lang="zh">
    <head>
        <meta charset="utf-8" />
        <title>FireBase Example</title>
        <script src="https://www.gstatic.com/firebasejs/4.9.1/firebase.js"></script>
        <script>
          // Initialize Firebase
          var config = {
            apiKey: "AIzaSyAOSPYpgn7T_bKa6VbCaSeQlsw-3p3zqDs",
            authDomain: "yiibai-firebase.firebaseapp.com",
            databaseURL: "https://yiibai-firebase.firebaseio.com/",
            projectId: "yiibai-firebase",
            storageBucket: "yiibai-firebase.appspot.com",
            messagingSenderId: "334522625008"
          };
          firebase.initializeApp(config);

        var provider = new firebase.auth.GoogleAuthProvider();

        function googleSignin() {
           firebase.auth()

           .signInWithPopup(provider).then(function(result) {
              var token = result.credential.accessToken;
              var user = result.user;

              console.log(token)
              console.log(user)
           }).catch(function(error) {
              var errorCode = error.code;
              var errorMessage = error.message;

              console.log(error.code)
              console.log(error.message)
           });
        }

        function googleSignout() {
           firebase.auth().signOut()

           .then(function() {
              console.log('Signout Succesfull')
           }, function(error) {
              console.log('Signout Failed')  
           });
        }
        </script>
    </head>
<body>
    <button onclick = "googleSignin()">使用Google賬號(hào)登錄</button>
    <button onclick = "googleSignout()">Google賬號(hào)注銷</button>
</body>
</html>

刷新頁(yè)面后,可以點(diǎn)擊使用Google賬號(hào)登錄按鈕觸發(fā)Google彈出窗口。 如果登錄成功,開(kāi)發(fā)者控制臺(tái)將登錄指定的用戶賬號(hào)。

也可以點(diǎn)擊Google賬號(hào)注銷按鈕從應(yīng)用程序注銷。控制臺(tái)將顯示確認(rèn)注銷成功。