android - FirebaseAuthUserCollisionException? -
i using firebase documentation use facebook login android application. able login users using google , twitter. when click on login facebook, button changing logout button. should redirect login success activity, because configured firebaseauth.authstatelistener
.
i posting activity
here, please let me know if doing wrong.
public class mainactivity extends appcompatactivity implements googleapiclient.onconnectionfailedlistener { private static final string tag = "mainactivity"; private static final int google_rc_sign_in = 9001; private static final int twitter_rc_sign_in = 140; private firebaseauth mauth; private firebaseauth.authstatelistener mauthstatelistener; private googleapiclient mgoogleapiclient; callbackmanager callbackmanager; @bindview(r.id.google_signin_button) signinbutton mgooglesigninbutton; @bindview(r.id.facebook_login_button) loginbutton mfacebookloginbutton; @bindview(r.id.twitter_login_button) twitterloginbutton mtwitterloginbutton; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); twitterauthconfig authconfig = new twitterauthconfig(getstring(r.string.twitter_consumer_key), getstring(r.string.twitter_consumer_secret)); fabric.with(this, new twitter(authconfig)); facebooksdk.sdkinitialize(this); setcontentview(r.layout.activity_main); butterknife.bind(mainactivity.this); googlesigninoptions gso = new googlesigninoptions.builder(googlesigninoptions.default_sign_in) .requestidtoken(getstring(r.string.default_web_client_id)) .requestemail() .build(); mgoogleapiclient = new googleapiclient.builder(this) .enableautomanage(this,this) .addapi(auth.google_sign_in_api,gso) .build(); mauth = firebaseauth.getinstance(); mauthstatelistener = new firebaseauth.authstatelistener(){ @override public void onauthstatechanged(@nonnull firebaseauth firebaseauth) { firebaseuser user = firebaseauth.getcurrentuser(); if (user != null) { //user signed in log.v(tag, "yo baby"); log.d(tag,"name"+user.getdisplayname()); gotohome(); } else { //do logic } } }; mgooglesigninbutton.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent signinintent = auth.googlesigninapi.getsigninintent(mgoogleapiclient); startactivityforresult(signinintent, google_rc_sign_in); } }); callbackmanager = callbackmanager.factory.create(); mfacebookloginbutton.setreadpermissions("email", "public_profile"); mfacebookloginbutton.registercallback(callbackmanager, new facebookcallback<loginresult>() { @override public void onsuccess(loginresult loginresult) { handlefacebookauthentication(loginresult.getaccesstoken()); } @override public void oncancel() { } @override public void onerror(facebookexception error) { } }); mtwitterloginbutton.setcallback(new callback<twittersession>() { @override public void success(result<twittersession> result) { handletwitterauthentication(result.data); } @override public void failure(twitterexception exception) { } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == google_rc_sign_in){ googlesigninresult result = auth.googlesigninapi.getsigninresultfromintent(data); if (result.issuccess()) { googlesigninaccount account = result.getsigninaccount(); handlegoogleauthentication(account); } else { //google login failed } } else if (requestcode == twitter_rc_sign_in) { mtwitterloginbutton.onactivityresult(requestcode,resultcode,data); } else { callbackmanager.onactivityresult(requestcode,resultcode,data); } } private void handlegoogleauthentication(googlesigninaccount account) { authcredential credential = googleauthprovider.getcredential(account.getidtoken(), null); mauth.signinwithcredential(credential).addoncompletelistener(this, new oncompletelistener<authresult>() { @override public void oncomplete(@nonnull task<authresult> task) { if (task.issuccessful()) { //save credentials in google smart lock } else { // } } }); } private void handlefacebookauthentication(accesstoken accesstoken) { authcredential credential = facebookauthprovider.getcredential(accesstoken.gettoken()); mauth.signinwithcredential(credential).addoncompletelistener(this, new oncompletelistener<authresult>() { @override public void oncomplete(@nonnull task<authresult> task) { if (task.issuccessful()) { //save credentials in google smart lock } else { } } }); } private void handletwitterauthentication(twittersession session) { authcredential credential = twitterauthprovider.getcredential(session.getauthtoken().token, session.getauthtoken().secret); mauth.signinwithcredential(credential).addoncompletelistener(this, new oncompletelistener<authresult>() { @override public void oncomplete(@nonnull task<authresult> task) { if (task.issuccessful()) { //save credentials in google smart lock } else { } } }); } private void gotohome() { startactivity(new intent(mainactivity.this, homeactivity.class)); finish(); return; } @override protected void onstart() { super.onstart(); mauth.addauthstatelistener(mauthstatelistener); } @override protected void onstop() { super.onstop(); if (mauthstatelistener != null) { mauth.removeauthstatelistener(mauthstatelistener); } } @override public void onconnectionfailed(@nonnull connectionresult connectionresult) { } }
update1
finally understood reason why facebook login not working. because logged app using google signin. since email google , facebook same giving firebaseauthusercollisionexception
. should have debugged application before post question in stackoverflow.
update2
i not closing question may faced situation me. , update solution firebaseauthusercollisionexception
, dig more it.
i faced same problem , how got around with.
if task not successful, i'm checking whether exception thrown instanceof firebaseauthusercollisionexception
. if yes, toast displayed user saying 'account email exists!'.
private void handlefacebookaccesstoken(final accesstoken token) { log.d(tag, "handlefacebookaccesstoken:" + token); authcredential credential = facebookauthprovider.getcredential(token.gettoken()); mauth.signinwithcredential(credential) .addoncompletelistener(this, new oncompletelistener<authresult>() { @override public void oncomplete(@nonnull task<authresult> task) { if (!task.issuccessful()) { log.w(tag, "signinwithcredential", task.getexception()); toast.maketext(getapplicationcontext(), "firebase facebook login failed", toast.length_short).show(); if(task.getexception() instanceof firebaseauthusercollisionexception) { toast.maketext(getapplicationcontext(), "user email id exists", toast.length_short).show(); } loginmanager.getinstance().logout(); } } }); }
credit: check if given email exists
Comments
Post a Comment