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: "zyiz-firebase.firebaseapp.com",
            databaseURL: "https://zyiz-firebase.firebaseio.com/",
            projectId: "zyiz-firebase",
            storageBucket: "zyiz-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账号注销按钮从应用程序注销。控制台将显示确认注销成功。


上一篇:Firebase使用Google账号登录

下一篇:Firebase匿名身份验证

关注微信小程序
程序员编程王-随时随地学编程

扫描二维码
程序员编程王

扫一扫关注最新编程教程