2

The admin can only add a new user. My problem is when I created a new user, it automatically signin that user then it signouts the admin.
FYI. I didn't use the Admin SDK. Firebase

My code:

               var password = $('#row' + x + 'cell1').val();
                var fname = $('#row' + x + 'cell2').val();
                var email = teacherID + "@gmail.com";
                console.log(email + password);

                firebase.auth().createUserWithEmailAndPassword(email, password).then(function(user) {
                //Add to users table
                var pushedUser = firebase.database().ref('users/' + user.uid).set({ role: "Teacher",
                fullname: fname });  
                 //Add name and default dp to the Authorization table
                    user.updateProfile({
                        displayName: fname,
                        photoURL: "default_dp",
                    });
                var $rowFirst = $("table:first").find("tr:first");
                var $rowNext = $('#tblCSVaccounts tr').eq(x);
                //get all td's in row
                var $tdsFirst = $rowFirst.find("td");
                // loop all td    
                $.each($tdsFirst, function(i, el) {
                    //get value of each td
                    var txtEntity = $(this).find("input").val();
                    var $rowNext = $('#tblCSVaccounts tr').eq(x);
                    var $tdsNext = $rowNext.find("td:eq(" + i + ")");

                        $.each($tdsNext, function(i, el) {
                            //get value of each td
                            var txtValue = $(this).find("input").val();
                            //Update in database
                            const mySection = $('#sectionUpload').val();
                            var pushedRef = firebase.database().ref('Teachers/' + user.uid).update({ [txtEntity]: txtValue }); 
                            $('#alert-success').removeClass('hide');
                        });
                    });
                    }, function(error) {
                        // An error happened.
                        var errorCode = error.code;
                        var errorMessage = error.message;
                    });
Mekeni
  • 23
  • 4
  • Please add the code for creating a new user. – Hareesh Dec 19 '17 at 13:31
  • @Hareesh added the code :) – Mekeni Dec 20 '17 at 00:43
  • Please refer to this answer [link](https://stackoverflow.com/questions/37517208/firebase-kicks-out-current-user/38013551#38013551) – Hareesh Dec 20 '17 at 04:45
  • 1
    If you are building an admin UI for creating users, you are better off using the Firebase Admin SDK instead. They provide a `createUser` API (https://firebase.google.com/docs/auth/admin/manage-users#create_a_user) to facilitate this. The client side API you are using is meant for the end user. When a user signs up, they get signed in directly. – bojeil Dec 21 '17 at 00:01

1 Answers1

0

you can use firebase.auth().onAuthStateChanged(...) hope its help you, guy!

Robert
  • 21
  • 3