Unable to write user data in firebase

I am new to this, so bear with me. I am trying to use simple access to the base of lights using email and password. I have a job with this:

var authClient = new FirebaseAuthClient(fireRef, function(error, user) { if (error) { // an error occurred while attempting login if(error.code === "INVALID_EMAIL"){ $('#log_email_error').hide(); $('#log_pass_error').hide(); $('#log_email_error').html("Invalid email specified.").fadeIn(); $('.login_button').removeClass('login_button_success').attr('value','Log in'); } if (error.code === "INVALID_PASSWORD") { $('#log_email_error').hide(); $('#log_pass_error').hide(); $('#log_pass_error').html("The specified password is incorrect..").fadeIn(); $('.login_button').removeClass('login_button_success').attr('value','Log in'); } console.log(error); } else if (user) { // user authenticated with Firebase hideLogin(); $('.userInfo_cont').show(); $('.userInfo').html('<div> '+ user.email + ' <span class="grey">| </span> </div>'); $('.logout').on('click',function(){ authClient.logout(); }); console.log('User ID: ' + user.id + ', Provider: ' + user.provider); } else { // user is logged out $('.userInfo_cont').hide(); showLogin(); } }); 

But when the user logs in, I want to save additional information in the firebase / users area, what can I do with this during registration:

 $('#submit_reg').on('click',function(){ var firstName = $('#regFirstname').val(); var lastName = $('#regLastname').val(); var email = $('#regUsername').val(); var password = $('#regPassword').val(); authClient.createUser(email, password, function(error, user) { if (!error) { console.log('User Id: ' + user.id + ', Email: ' + user.email); authClient.login('password', { email: email, password: password, rememberMe: false }); hideLogin(); userInfo = { userId : user.id, firstName : firstName, lastName : lastName, email : user.email } var url = USERS_LOCATION + "/" + user.id; var userRef = new Firebase(url); console.log(userInfo); userRef.set(userInfo); }else{ //display error alert(error); } }); }); 

My problem is when I implement reading rules such as documentation:

 { "rules": { "users":{ "$userid": { ".read": "auth.id == $userid", ".write": "auth.id == $userid" } } } } 

I get permission denied when a user logs in. Thus, it logs the user just fine, but does not write additional data to the / users / id area.

Any help would be greatly appreciated.

Craig

+4
source share
1 answer

In the above snippet, you call login() and then immediately call set() . This is problematic because the login() method is asynchronous, and you can almost always call the set() method before returning the login attempt method, because login() does not block, but makes a network call to Firebase Servers.

This means that even if you call login() with the correct email address and password, you are trying to set the data before the authentication process is complete.

I would recommend moving your set() logic to a block that will be executed only when you are sure that the user has already authenticated, for example, in the callback that you passed when you called new FirebaseAuthClient() , and found a registered user.

+5
source

Source: https://habr.com/ru/post/1468816/


All Articles