0

I'm having some trouble authenticating with a factory in my app. The goal is to get a token only if the user has not already been authenticated. When I use the code below, I get the following error:

Cannot read property 'uid' of null

I can't tell if this is just a minor syntax issue or something bigger. It looks like none of the code starting with var authData is being read.

(function() {
  'use strict';
   angular.module('firebase.auth', ['firebase', 'firebase.ref'])

   .factory('Auth', ['$firebaseAuth', 'Ref', function ($firebaseAuth, Ref) {

        return $firebaseAuth(Ref);
        var authData = Ref.getAuth();
        console.log(authData);

         if (authData) {
            console.log('already logged in with ' + authData.uid);
          } else {

            return Auth.$authAnonymously({rememberMe: true}).then(showError);

             function showError(err) {
              Login.err = err;
            }
          }
        }
    ]);
})();

angular.module('App')

   .factory('Projects', function ($firebaseArray, fbURL, Auth, Ref) {
        var authData = Ref.getAuth();
        var ref = new Firebase(fbURL + '/projects/' + authData.uid);
          return $firebaseArray(ref);
})

1 Answer 1

1
 return $firebaseAuth(Ref);

It looks like none of the code starting with var authData is being read.

You stuck a return statement at the top of your function. That will exit the function.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. Does this mean I need to use a new factory for the code that starts with var authData, or is there a better way to accomplish my goal here?
I'm not entirely sure what your goal is here. Can you give a little background explanation of what $firebaseAuth is and why you might need to return it at the top like you are?
I'm using Firebase as my backend. AngularFire is its AngularJS library. All of this is very new to me, but I think $firebaseAuth is a service and the returned object contains methods for authenticating users. I thought I needed to put it at the top in order to use getAuth() to retrieve the current authentication state (to determine whether the user has already been authenticated and authenticate anonymously, if need be). Does that make sense?
Hi Ken. A factory is supposed to create something and return it. In this case, you're creating the service. If you want to manipulate it in some way, then simply say var myService = $firebaseAuth(Ref) and use myService when you want to access $firebaseAuth. Make sure, at the bottom, you return myService.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.