简体   繁体   English

如果注册,请登录用户(Firebase Facebook / google auth)

[英]Sign in user if is register (Firebase facebook/google auth)

i´m want to check if the user who want to sign in using the facebook or google auth in to my web app is register on the real time database of firebase, so the idea is after the user press the button of sign in with facebook/google, first check in to the real time database if the uid is already on the real time database before redirect the user to another URL, for this i´m using the next function: 我想检查要使用Facebook或google auth登录到我的Web应用程序的用户是否已在firebase的实时数据库中注册,所以该想法是在用户按下以Facebook登录的按钮之后/ google,首先将实时数据库中的uid检查到实时数据库中,然后再将用户重定向到另一个URL,为此,我使用下一个函数:

  app.auth = function(){
    firebase.auth().onAuthStateChanged(function(user) {
      if (user) {
        var users_ref = firebase.database().ref('dream_wedding_users');
        var register_user;
        var register = false;
        users_ref.on('value', function(snapshot) {
          register_user = snapshot.val();
        });
        $.each(register_user, function(index, val){
          if(user.uid === index){
            register = true;
          }
        })
        if(!register){
          firebase.auth().signOut();
          $('#login').modal('hide');
          $('#modal_register').modal('show');
          return false;
        }else{
          $(window).attr('location', 'http://localhost/wedding/en/gallery_en.php');
        }
      }
    });
  }

and for the auth function just the regular auth function for facebook and google that attach to a button. 对于auth函数,仅是附加到按钮的facebook和google的常规auth函数。

  app.facebook_login = function(){
    var provider = new firebase.auth.FacebookAuthProvider();
    firebase.auth().signInWithPopup(provider).then(function(result) {
      // This gives you a Facebook Access Token. You can use it to access the Facebook API.
      var token = result.credential.accessToken;
      // The signed-in user info.
      var user = result.user;
      // ...
    }).catch(function(error) {
      console.log(error)
    });
  }

  app.google_login = function(){
    var provider = new firebase.auth.GoogleAuthProvider();
    firebase.auth().signInWithPopup(provider).then(function(result) {
      // This gives you a Google Access Token. You can use it to access the Google API.
      var token = result.credential.accessToken;
      // The signed-in user info.
      var user = result.user;
      // ...
      firebase.auth().signInWithRedirect(provider);
    }).catch(function(error) {
      console.log(error);
    });
  }

my problem is the next one, when i click sign in facebook or google, first login the user, then redirect the user, then check is the user is register on the real time data base and then logout the user if is not register and then show the modal. 我的问题是下一个,当我单击“登录” facebook或google时,首先登录用户,然后重定向用户,然后检查用户是否已在实时数据库中注册,如果未注册则注销用户,然后显示模态。 i don´t want that redirect the user i want that check if the user is register and then show the modal of "register" if the user is not register without redirect, and redirect if the user is register. 我不希望重定向用户我要检查用户是否注册,如果用户没有重定向就没有注册,则显示“注册”的模式,如果用户是注册,则显示重定向。

You want to add a unique user , if it is already not registered right ? 您想添加一个唯一用户,如果该用户尚未注册吗? here is the valid way: 这是有效的方法:

var usernew = result.additionalUserInfo.isNewUser
console.log('Is this a new user ? => ' + usernew );

complete code is as follows: 完整的代码如下:

var provider = new firebase.auth.GoogleAuthProvider();
firebase.auth().signInWithPopup(provider).then(function(result){
    var usernew = result.additionalUserInfo.isNewUser;
    console.log('Is this a new user ? => ' + usernew );
    if (usernew == true) {
        NewUser();
    }
    var token = result.credential.accessToken;       
    user = result.user; 
    //  alert(JSON.stringify(user.photoURL))                                                      
}).catch(function(error) {
    var errorCode = error.code;
    var errorMessage = error.message;
    var email = error.email;
    var credential = error.credential;
});

add user if it is not registered: 如果尚未注册,请添加用户:

function NewUser(){
    firebase.database().ref('/users').push({
        Name : 'Johnson'
        Age : '22'
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM