I have a small web site where users can sign in on Firebase, using email and password.
I uses this code:
<script>
function SignUpWithMailPSW(email,pswRdmSeq) {
....
firebase.auth().createUserWithEmailAndPassword(email, pswRdmSeq).then(function(user) {
....
}
... useful things irrelevant to the question.
}
</script>
Now users can also sign in using Facebook.
To get started I found this document. And to experiment I did something based on what I read, just a small test page. Here is the code:
<HTML>
<HEAD>
<META HTTP-EQUIV="content-type" CONTENT="text/html; CHARSET=UTF-8">
<script src="https://www.gstatic.com/firebasejs/6.3.5/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.3.5/firebase-auth.js"></script>
</HEAD>
<BODY>
<script>
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "ABCDEF-APIKEY-12345",
authDomain: "myapp.firebaseapp.com",
databaseURL: "https://myapp.firebaseio.com",
projectId: "myapp",
storageBucket: "myapp.appspot.com",
messagingSenderId: "123456789",
appId: "1:987654321:web:xy2122k98xyxy8988"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
</script>
<b>TEST ONLY PAGE !!!</b>
<script>
var provider = new firebase.auth.FacebookAuthProvider();
function SignUpWithFB() {
// var provider = new firebase.auth.FacebookAuthProvider();
firebase.auth().signInWithPopup(provider).then(function(result) {
// This gives you a Facebook Access Token. You can use it to access the Facebook API.
var token = result.credential.accessToken;
// The signed-in user info.
var user = result.user;
// ...
}).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;
// ...
});
}
SignUpWithFB();
</script>
<b>TEST ONLY PAGE !!!</b>
</BODY>
</HTML>
At this point it is starting to work.
But I would like to know how to make use of the result (and result.credential) parameter that we get in the call back function (or promise?):
firebase.auth().signInWithPopup(provider).then(function(result) {});
I did not find anything significant about this result variable (type, fields, use, ..etc..) searching the net.
Some hints would be very useful.
2
Answers
The result variable which you are getting in the then part of the promise , is an object (type) holding all the information about the user. And contains a lot of properties on itself, using which you can get all of these data very easily.
Naming a few important ones
Note : To list and check all these property , try outputting it in console
console.log(result);
The
signInWithPopup
method is declared as:So it returns
Promise<UserCredential>
. This means that in yourthen
callback you get aUserCredential
object, which is declared as:So it has a
user
and acredential
property, and may also haveadditionalUserInfo
andoperationType
.For more information on each of these, keep following the links in the reference documentation.