简体   繁体   English

有没有办法获得firebase认证的github用户的用户名

[英]Is there a way to get a firebase-authenticated github user's username

I'm essentially trying to have a website which authenticates the user's GitHub account through firebase, and then saves their username in a database - I specifically need the username for this. 我基本上试图建立一个通过firebase验证用户GitHub帐户的网站,然后将其用户名保存在数据库中 - 我特别需要用户名。 I have the authentication working but it seems that firebase doesn't have a way to access the username, only things such as the email, display name, etc. Currently I just want to save the username as a variable 我的身份验证工作,但似乎firebase没有办法访问用户名,只有电子邮件,显示名称等等。目前我只想将用户名保存为变量

I've come across this: https://developer.github.com/v3/users/#get-the-authenticated-user I assume "login" is the username? 我遇到过这个问题: https//developer.github.com/v3/users/#get-the-authenticated-user我假设“login”是用户名? I'm relatively new to things though, and can't find any clear examples of how I would access this information with my token from firebase. 我对事情比较陌生,但是找不到任何关于我如何使用来自firebase的令牌访问此信息的明确示例。 For reference, I have an index.html, linked to app.js, which contains all of my authentication code. 作为参考,我有一个index.html,链接到app.js,其中包含我的所有身份验证代码。

var provider = new firebase.auth.GithubAuthProvider();
    provider.addScope('read:user');
    //get elements
    const authenticateBtn = document.getElementById('authbtn');

    //add login event
    authenticateBtn.addEventListener('click', e=>{

        firebase.auth().signInWithPopup(provider).then(function(result) {
        // This gives you a GitHub Access Token. You can use it to access the GitHub API.
        var token = result.credential.accessToken;

        // The signed-in user info.
        var user = result.user;

        //what I want to get
        //var Username = ; 

        //some data I am able to get
        var displayName = user.displayName;
        var email = user.email;
        var emailVerified = user.emailVerified;
        var photoURL = user.photoURL;
        var isAnonymous = user.isAnonymous;
        var uid = user.uid;
        var providerData = user.providerData;

        //where I want to print the username
        //console.log(userName);


        // ...
        }).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // The email of the user's account used.
        var email = error.email;
        // The firebase.auth.AuthCredential type that was used.
        var credential = error.credential;
        // ...
        });
    })

I really just need a beginners explanation of if what I want to do is possible and if so, exactly what code I'd need where in my project. 我真的只需要一个初学者的解释,如果我想做什么是可能的,如果是的话,确切地说我需要在我的项目中使用哪些代码。 Thanks! 谢谢!

So I ended up getting some help from a friend, here is my solution: 所以我最终得到了朋友的帮助,这是我的解决方案:

firebase.auth().signInWithPopup(provider).then(function(result) {

        // This gives you a GitHub Access Token. You can use it to access the GitHub API.
        var token = result.credential.accessToken;
        // The signed-in user info.

        var user = result.user;
        var username;
        var obj;

        const Http =  new XMLHttpRequest();
        Http.open("GET", "https://api.github.com/user?access_token="+token);
        Http.send();

        Http.onreadystatechange=function(){
            if(this.readyState==4 && this.status==200){
                obj = JSON.parse(Http.responseText);
                username = obj.login;
                console.log(username);

            }
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase:成功通过电子邮件登录后,如何使用Firebase API获取经过身份验证的用户令牌? - Firebase: How to get authenticated user's token using firebase api after successfully logging in with email? 检查已验证用户的用户名,然后将用户名传递到javascript中? - check username of authenticated user then pass the username into javascript? Firebase-如何限制通过身份验证的用户对某些路径的访问? - Firebase - how to restrict authenticated user's access to certain paths? 如何让提供商从Firebase经过身份验证的用户访问令牌? - How to get providers access tokens from Firebase authenticated user? 如何使用Meteor获取当前用户的用户名 - How to get current user's username with Meteor Firebase 跟踪用户是否经过身份验证 - Firebase keep track if is user authenticated 从Web应用程序中删除经过Firebase身份验证的用户 - delete firebase authenticated user from web application 如何获取在 Firebase Authentication for Web 中对当前用户进行身份验证的身份验证提供程序? - How to get the authentication provider which authenticated the current user in Firebase Authentication for Web? 有没有办法获得 Firebase Auth 用户 UID? - Is there any way to get Firebase Auth User UID? 如何从 angular 中的 firebase 中删除经过身份验证的用户 - How to delete authenticated user from firebase in angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM