Firebase kicks out current user

前端 未结 14 1996
一个人的身影
一个人的身影 2020-11-22 01:42

So I have this issue where every time I add a new user account, it kicks out the current user that is already signed in. I read the firebase api and it said that \"I

14条回答
  •  执笔经年
    2020-11-22 02:23

    Android solution (Kotlin):

    1.You need FirebaseOptions BUILDER(!) for setting api key, db url, etc., and don't forget to call build() at the end

    2.Make a secondary auth variable by calling FirebaseApp.initializeApp()

    3.Get instance of FirebaseAuth by passing your newly created secondary auth, and do whatever you want (e.g. createUser)

        // 1. you can find these in your project settings under general tab
        val firebaseOptionsBuilder = FirebaseOptions.Builder()
        firebaseOptionsBuilder.setApiKey("YOUR_API_KEY")
        firebaseOptionsBuilder.setDatabaseUrl("YOUR_DATABASE_URL")
        firebaseOptionsBuilder.setProjectId("YOUR_PROJECT_ID")
        firebaseOptionsBuilder.setApplicationId("YOUR_APPLICATION_ID") //not sure if this one is needed
        val firebaseOptions = firebaseOptionsBuilder.build()
    
        // indeterminate progress dialog *ANKO*
        val progressDialog = indeterminateProgressDialog(resources.getString(R.string.progressDialog_message_registering))
        progressDialog.show()
    
        // 2. second auth created by passing the context, firebase options and a string for secondary db name
        val newAuth = FirebaseApp.initializeApp(this@ListActivity, firebaseOptions, Constants.secondary_db_auth)
        // 3. calling the create method on our newly created auth, passed in getInstance
        FirebaseAuth.getInstance(newAuth).createUserWithEmailAndPassword(email!!, password!!)
        .addOnCompleteListener { it ->
    
            if (it.isSuccessful) {
    
                // 'it' is a Task, so we can get our newly created user from result
                val newUser = it.result.user
    
                // store wanted values on your user model, e.g. email, name, phonenumber, etc.
                val user = User()
                user.email = email
                user.name = name
                user.created = Date().time
                user.active = true
                user.phone = phone
    
                // set user model on /db_root/users/uid_of_created_user/, or wherever you want depending on your structure
                FirebaseDatabase.getInstance().reference.child(Constants.db_users).child(newUser.uid).setValue(user)
    
                // send newly created user email verification link
                newUser.sendEmailVerification()
    
                progressDialog.dismiss()
    
                // sign him out
                FirebaseAuth.getInstance(newAuth).signOut()
                // DELETE SECONDARY AUTH! thanks, Jimmy :D
                newAuth.delete()
    
            } else {
    
                progressDialog.dismiss()
    
                try {
    
                    throw it.exception!!
    
                    // catch exception for already existing user (e-mail)
                } catch (e: FirebaseAuthUserCollisionException) {
    
                    alert(resources.getString(R.string.exception_FirebaseAuthUserCollision), resources.getString(R.string.alertDialog_title_error)) {
    
                        okButton {
    
                            isCancelable = false
    
                        }
    
                    }.show()
    
                }
    
            }
    
        }
    

提交回复
热议问题