简体   繁体   中英

nil is not defined while checking if user is logged in

I want to use Firebase to detect if user is logged in or not:

var auth = new FirebaseSimpleLogin(Ref, function(err, user){
        if(err){
          console.log(err);
        }else if(user){
          console.log("User ID:" + user.id + ", Email: " + user.email);
        }else if(user == nil){
          console.log("No user logged in");             
        }else{
          login();
        }
      });

But it gives me this error:

nil is not defined

when user is not logged in. Any explanation and how to fix that?

You need to use getAuth() method and do the following:

  var user = ref.getAuth();

Firebase will return an object and then you can do something like this to see if the user is logged logged in:

  if (user==null) {
      //user not logged in
  }; 

There is no nil keyword in JavaScript, though there is null and undefined . Try this approach, from the Firebase Simple Login documentation :

var auth = new FirebaseSimpleLogin(ref, function(error, user) {
  if (error) {
    // an error occurred while attempting login
    console.log(error);
  } else if (user) {
    // user authenticated with Firebase
    console.log('User ID: ' + user.id + ', Provider: ' + user.provider);
  } else {
    // user is logged out
  }
});

For Swift try this,

let FIREBASE_REF = Firebase(url: "FirebaseURL")

    if let _ = FIREBASE_REF.authData{
      //Use logged in
    }else{
     //Use not logged in
    }

When you instantiate a new FirebaseSimpleLogin object it may contain a property of 'user'.

If this property is defined, it should contain user information and therefore indicate the logged in status of a user.

Before a user logs in, the user property is null.

The following example shows a simple function that returns a boolean true value if the user is logged in, and false if not.

var ref = new Firebase('https://YOUR-FIREBASE-URL.firebaseio.com/');
var auth = new FirebaseSimpleLogin(ref, function(error, user) {
    if (error) {
        // Handle error
    }
    else if (user) {
       // Do something with logged in user information
    }
});

function loggedIn() {
    return auth.user !== null;
};

As you can see, calling loggedIn() will only return true if auth.user is defined.

If you want to check whether no user is logged in, you can create a function that does the opposite of the above. Like so.

function noUserloggedIn() {
    return auth.user == null;
};

The above function will return true if auth.user is NOT defined ie auth.user == null, indicating that there aren't any logged in users.

And for completeness, and as mentioned in other comments / answers, you need to replace

nil

with

null

as the nil keyword does not exist in javascript.

UPDATED CODE - I've just updated my code to reflect David's comment, the constructor does indeed require a callback, my original code was converted from angularFire using $firebaseAuth which took a ref as an argument and returned a promise I believe. It didn't require the callback, hence me leaving it out :), this callback however is fired everytime a users authentication state has changes.

From the github docs:

To initialize Simple Login we need to create a FirebaseSimpleLogin object. This object takes in a Firebase reference and a callback function. The callback is triggered any time that the user's authentication state is changed.

Source: https://github.com/firebase/firebase-simple-login/tree/master/docs/v1/

However the above has now been deprecated, as Firebase Login is now bundled with FireBase core. check https://www.firebase.com/docs/web/api/firebasesimplelogin/ for details.

Also Firebase has moved on a lot since this answer. I suggest looking at the new API's here https://www.firebase.com/docs/

This should also work.

signedIn: function(){
  return !!Auth.user.provider;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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