1

I am using Angularfire for my login system and trying to get current logged in user's username for purpose of using in controller. Firts getting uid from $getAuth and sync it with data in database. But what I am trying below is not working.

.factory("Auth", function($firebaseAuth) {
var ref = new Firebase("URL");
return $firebaseAuth(ref);
})

.factory("Konusma", function($firebaseObject) { //When you give user's uid to this factory its gives you user data in database.
return function(xm) {
        var ref = new Firebase("URL");
        var userdata = ref.child('users').child(xm);
        return $firebaseObject(userdata);
}})

.controller("chatCtrl", function(Auth, Konusma) {

var userx = Auth.$getAuth(); //Current logged in user's uid and email

var chatci = function() { //Trying to get current logged in user's username and other data according to uid which is comes from Auth.$getAuth.

        Konusma(userx.uid).$loaded().then(function(data){
        return data.username;
    });

};

enter image description here

Nasuh
  • 355
  • 3
  • 20
  • Can you add snapshot of your data? – giladk Nov 11 '15 at 11:23
  • Where do you use chatci function? – giladk Nov 11 '15 at 13:57
  • @giladk Just `currentUser = chatci` I want to return with current logged in user's username. – Nasuh Nov 11 '15 at 14:05
  • When you call `getAuth()` the user is likely not authenticated yet. You should instead wait for the user to be authenticated and only then access their data. I recently answered this question, so will mark this as a duplicate. – Frank van Puffelen Nov 11 '15 at 14:41
  • Possible duplicate of [Firebase email authentication with rules not working as expected](http://stackoverflow.com/questions/33600633/firebase-email-authentication-with-rules-not-working-as-expected) – Frank van Puffelen Nov 11 '15 at 14:41
  • @Frank van Puffelen How is this dublicate? My topic related to AngularJS and related to functions and promises. I dont understand anything that dublicate question. And this controller restrict to logged in users only. Clients has no access to this controller. – Nasuh Nov 11 '15 at 15:01
  • The problem is the same: you're not waiting for the user to be authenticated. In regular JavaScript that is accomplished with a callback, in AngularFire it's a promise that resolves. Feel free to disagree, but the cause of your problem is the same. – Frank van Puffelen Nov 11 '15 at 15:07
  • @Frank van Puffelen Okay. I remove the tag `firebase` and wait for angularjs users will answer. So remove the dublicate tag please. – Nasuh Nov 11 '15 at 15:10
  • Instead of worrying about the semantics of a duplicate, I'd recommend you focus on solving your problem. You will need to wait for the user to authenticate before you try to access their data. `$getAuth()` does not wait. See http://stackoverflow.com/questions/33600633/firebase-email-authentication-with-rules-not-working-as-expected or http://stackoverflow.com/questions/31227142/angularfire-the-difference-between-waitforauth-and-requireauth or http://stackoverflow.com/questions/23484381/in-angularfire-why-is-the-auth-user-property-null-in-the-controller-but-not-th – Frank van Puffelen Nov 11 '15 at 15:26

1 Answers1

0

As mentioned in comments -

You are trying to get the data before user is authenticated.

So in your controller , instead of :

 var userx = Auth.$getAuth();

you can use this :

var userx = {};
Auth.$onAuth(function(authData) {
  var userx = authData;
  // or bind to scope - 
  // $scope.authData = authData;
});

this will also update your authData variable every time auth status is change.

Anyway - you still need to call: currentUser = chatci() only after user is loggedIn , and not evrey time that auth is change.

The best way is to use it with router, see here : https://www.firebase.com/docs/web/libraries/angular/guide/user-auth.html#section-routers

So the controller is loaded only after auth is success.

But for your example - you can add this to your controller:

Auth.$waitForAuth().then(function(authData){
   if (authData){
        currentUser = chatci()
   }
});
giladk
  • 176
  • 6
  • But when user is in this page he is already authenticated because this only displays to logged in user. I think my problem is `var chatci = function `. Because that that running the function. Its gives me text. – Nasuh Nov 11 '15 at 15:51