'linking accounts firebase upon new authentication
my app contains Facebook, google and email and password authentication for example, if a user sign in with Facebook and signs out and next time signs in with google or email password authentication it links the account with previous Facebook authentication assuming the email is the same
how can I achieve it?
Updated:
package com.example.fypauthentication
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Button
import android.widget.Toast
import com.facebook.*
import com.facebook.appevents.AppEventsLogger;
import com.facebook.login.LoginManager
import com.facebook.login.LoginResult
import com.facebook.login.widget.LoginButton
import com.google.android.gms.auth.api.signin.GoogleSignIn
import com.google.android.gms.auth.api.signin.GoogleSignInClient
import com.google.android.gms.auth.api.signin.GoogleSignInOptions
import com.google.android.gms.common.api.ApiException
import com.google.firebase.auth.FacebookAuthProvider
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.auth.GoogleAuthProvider
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase
import java.util.*
class MainActivity : AppCompatActivity() {
lateinit var callbackManager : CallbackManager
lateinit var auth: FirebaseAuth
lateinit var login : Button
//Google
private lateinit var googleSignInClient: GoogleSignInClient
lateinit var sign_in_btn: Button
companion object {
private const val RC_SIGN_IN = 120
}
//Google
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//Google
sign_in_btn = findViewById<Button>(R.id.sign_in_btn)
// Configure Google Sign In
val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build()
googleSignInClient = GoogleSignIn.getClient(this, gso)
sign_in_btn.setOnClickListener {
signIn()
}
//Google
// Initailization Facebook SDK
FacebookSdk.sdkInitialize(this@MainActivity)
auth = FirebaseAuth.getInstance()
callbackManager = CallbackManager.Factory.create()
//val loginButton : LoginButton = findViewById<LoginButton>(R.id.login_button)
login = findViewById<Button>(R.id.login)
login.setOnClickListener(object : View.OnClickListener {
override fun onClick(view: View?) {
// Do some work here
LoginManager.getInstance().logInWithReadPermissions(this@MainActivity,Arrays.asList("email","public_profile"))
LoginManager.getInstance().registerCallback(callbackManager, object : FacebookCallback<LoginResult> {
override fun onSuccess(loginResult: LoginResult) {
handleFacebookAccessToken(loginResult.accessToken)
}
override fun onCancel() {
}
override fun onError(error: FacebookException) {
}
})
}
})
//loginButton.setReadPermissions("email", "public_profile")
}
public override fun onStart() {
super.onStart()
// Check if user is signed in (non-null) and update UI accordingly.
val currentUser = auth.currentUser
if(currentUser != null)
{
updateUI(currentUser)
}
}
// override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
// callbackManager.onActivityResult(requestCode, resultCode, data)
// super.onActivityResult(requestCode, resultCode, data)
// }
private fun handleFacebookAccessToken(token: AccessToken) {
val credential = FacebookAuthProvider.getCredential(token.token)
auth.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
val user = auth.currentUser
updateUI(user)
} else {
// If sign in fails, display a message to the user.
updateUI(null)
}
}
}
private fun updateUI(user: FirebaseUser?) {
if(user != null) {
val intent = Intent(this,WelcomeActivity::class.java)
startActivity(intent)
}
else {
}
}
//Google
private fun signIn() {
val signInIntent = googleSignInClient.signInIntent
startActivityForResult(signInIntent, RC_SIGN_IN)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data)
val exception = task.exception
if (task.isSuccessful) {
try {
// Google Sign In was successful, authenticate with Firebase
val account = task.getResult(ApiException::class.java)!!
Log.d("SignInActivity", "firebaseAuthWithGoogle:" + account.id)
firebaseAuthWithGoogle(account.idToken!!)
} catch (e: ApiException) {
// Google Sign In failed, update UI appropriately
Log.w("SignInActivity", "Google sign in failed", e)
}
} else {
Log.w("SignInActivity", exception.toString())
}
}
else {
callbackManager.onActivityResult(requestCode, resultCode, data)
super.onActivityResult(requestCode, resultCode, data)
}
}
private fun firebaseAuthWithGoogle(idToken: String) {
val credential = GoogleAuthProvider.getCredential(idToken, null)
auth.signInWithCredential(credential)
.addOnCompleteListener(this) { task ->
if (task.isSuccessful) {
// Sign in success, update UI with the signed-in user's information
Log.d("SignInActivity", "signInWithCredential:success")
val intent = Intent(this, WelcomeActivity::class.java)
startActivity(intent)
finish()
} else {
// If sign in fails, display a message to the user.
Log.d("SignInActivity", "signInWithCredential:failure")
}
}
}
//Google
}
this is what i have achived so far when i try to authenticate with google and facebook it works but for example if i try to authenticate with fb first than for the same email address if i try to authenticate for the google it does not work vice versa
i want to link first and last identifier because they have same email addresses don't want firebase to create two separate account if email is same for the google and facebook
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

