Firebase 教程

original icon
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.knowledgedict.com/tutorial/firebase-github-authentication.html

Firebase使用Github账号登录


在本章中,我们将介绍如何在 Firebase 中设置 Github 身份验证,使用 Github 账号认证登录。

第1步 - 启用 Github 身份验证
打开 Firebase 信息中心,然后点击左侧菜单中的身份验证。要打开可用方法的列表,需要在标签菜单中单击登录方法

现在可以从列表中选择 Github,启用它并保存。参考下图 -

第2步 - 创建 Github 应用程序

按照此链接创建 GitHub 应用程序。需要将 Firebase 中的回调网址复制到授权回调网址字段中。创建应用程序后,需要将客户端密钥和客户端密钥从 GitHub 应用复制到 Firebase。

第2步 - 创建登录按钮
创建一个文件:index.html,并将添加两个按钮。参考代码如下 -

<button onclick = "githubSignin()">使用 Github 账号登录</button>
<button onclick = "githubSignout()">Github 账号注销</button>

第3步 - 登录和退出
在这一步中,我们将创建用于登录和注销的两个函数:githubSigningithubSignout。这里将使用signInWithPopup()signOut()函数。

示例

让我们来看看下面的例子。参考函数的实现 -

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

function githubSignin() {
   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 githubSignout(){
   firebase.auth().signOut()

   .then(function() {
      console.log('Signout successful!')
   }, 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.GithubAuthProvider();

        function githubSignin() {
           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 githubSignout(){
           firebase.auth().signOut()

           .then(function() {
              console.log('Signout successful!')
           }, function(error) {
              console.log('Signout failed')
           });
        }
        </script>
    </head>
<body>
    <button onclick = "githubSignin()">使用 Github 账号登录</button>
    <button onclick = "githubSignout()">Github 账号注销</button>
</body>
</html>

刷新页面后,可以点击使用 Github 账号登录按钮触发 Github 弹出窗口。如果登录成功,开发者控制台将登录指定的用户账号。有关更新详细的账号认证信息,请查看文档: https://developer.github.com/v3/oauth/

也可以点击Github 账号注销按钮从应用程序注销。控制台将显示确认注销成功。