javascript - Handling linking accounts in Firebase -
i following firebase's instruction on social login. below example of using , working fine login authentication perspective.
i have, however, both google , facebook login working independently.
what able link accounts. can see below in fact might go (see comment):
if using multiple auth providers on app should handle linking user's accounts here.
i have tried many variations of think should go here, no avail. can guide me in relation think should go here? thanks!
function initfbapp() { // result redirect auth flow. // [start getidptoken] firebase.auth().getredirectresult().then(function (result) { if (result.credential) { // gives facebook access token. can use access facebook api. var token = result.credential.accesstoken; // [start_exclude] document.getelementbyid('fbquickstart-oauthtoken').textcontent = token; } else { document.getelementbyid('fbquickstart-oauthtoken').textcontent = 'null'; // [end_exclude] } // signed-in user info. var user = result.user; }).catch(function (error) { // handle errors here. var errorcode = error.code; var errormessage = error.message; // email of user's account used. var email = error.email; // firebase.auth.authcredential type used. var credential = error.credential; // [start_exclude] if (errorcode === 'auth/account-exists-with-different-credential') { alert('you have signed different auth provider emails.'); // if using multiple auth providers on app should handle linking // user's accounts here. } else { console.error(error); } // [end_exclude] }); // [end getidptoken] // listening auth state changes. // [start authstatelistener] firebase.auth().onauthstatechanged(function (user) { if (user) { // user signed in. 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; // [start_exclude] document.getelementbyid('fbquickstart-sign-in-status').textcontent = 'signed in'; document.getelementbyid('fbquickstart-sign-in').textcontent = 'log out'; document.getelementbyid('fbquickstart-account-details').textcontent = json.stringify(user, null, ' '); // [end_exclude] } else { // user signed out. // [start_exclude] document.getelementbyid('fbquickstart-sign-in-status').textcontent = 'signed out'; document.getelementbyid('fbquickstart-sign-in').textcontent = 'log in facebook'; document.getelementbyid('fbquickstart-account-details').textcontent = 'null'; document.getelementbyid('fbquickstart-oauthtoken').textcontent = 'null'; // [end_exclude] } // [start_exclude] document.getelementbyid('fbquickstart-sign-in').disabled = false; // [end_exclude] }); // [end authstatelistener] document.getelementbyid('fbquickstart-sign-in').addeventlistener('click', togglefbsignin, false); }
these steps on how handle auth/account-exists-with-different-credential: error if signing in new facebook account uses email of account exists. let's existing account google account.
you error in getredirectresult().catch(function(error) {})
the error contain email , credential field. need save credential (using recommended sessionstorage). check post more on that: firebase authentication javascript: setcookie pending credential redirect
you call firebase.auth().fetchprovidersforemail(error.email)
determine providers exist email.
you sign in 1 of existing providers , assert email same error.email. on success, load pending credential sessionstorage, re-initialize described in other post , link currentuser:
firebase.auth().currentuser.link(savedcred);
you have both accounts linked. keep in mind existing provider password type. in case don't need save credential, ask user password , sign them in using same email error.email. can call link directly error.credential.
btw, recommend firebaseui-web takes care of you: https://github.com/firebase/firebaseui-web
Comments
Post a Comment