Firebase kicks out current user

前端 未结 14 1800
一个人的身影
一个人的身影 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:22

    Here is a simple solution using web SDKs.

    1. Create a cloud function (https://firebase.google.com/docs/functions)
    import admin from 'firebase-admin';
    import * as functions from 'firebase-functions';
    
    const createUser = functions.https.onCall((data) => {
      return admin.auth().createUser(data)
        .catch((error) => {
          throw new functions.https.HttpsError('internal', error.message)
        });
    });
    
    export default createUser;
    
    1. Call this function from your app
    import firebase from 'firebase/app';
    
    const createUser = firebase.functions().httpsCallable('createUser');
    
    createUser({ email, password })
      .then(console.log)
      .catch(console.error);
    
    1. Optionally, you can set user document information using the returned uid.
    createUser({ email, password })
      .then(({ data: user }) => {
        return database
          .collection('users')
          .doc(user.uid)
          .set({
            firstname,
            lastname,
            created: new Date(),
          });
      })
      .then(console.log)
      .catch(console.error);
    
    0 讨论(0)
  • 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<AuthResult>, 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()
    
                }
    
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题