[英]Firebase: How to get authenticated user's token using firebase api after successfully logging in with email?
[英]Is there a way to get a firebase-authenticated github user's username
我基本上試圖建立一個通過firebase驗證用戶GitHub帳戶的網站,然后將其用戶名保存在數據庫中 - 我特別需要用戶名。 我的身份驗證工作,但似乎firebase沒有辦法訪問用戶名,只有電子郵件,顯示名稱等等。目前我只想將用戶名保存為變量
我遇到過這個問題: https : //developer.github.com/v3/users/#get-the-authenticated-user我假設“login”是用戶名? 我對事情比較陌生,但是找不到任何關於我如何使用來自firebase的令牌訪問此信息的明確示例。 作為參考,我有一個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;
// ...
});
})
我真的只需要一個初學者的解釋,如果我想做什么是可能的,如果是的話,確切地說我需要在我的項目中使用哪些代碼。 謝謝!
所以我最終得到了朋友的幫助,這是我的解決方案:
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.