0

I want to get some info about the currently logged in user, I have stored the variable in currentUser but nothing appears when I try to access it from my controller

authentication.service.js:

(function () {
  'use strict';

  angular
    .module('thinkster.authentication.services')
    .factory('Authentication', Authentication);


  Authentication.$inject = ['$cookies', '$http'];

  /**
  * @namespace Authentication
  * @returns {Factory}
  */
  function Authentication($cookies, $http) {
    /**
    * @name Authentication
    * @desc The Factory to be returned
    */


    var Authentication = {
      login: login,
      register: register,
      getAuthenticatedAccount: getAuthenticatedAccount,
      isAuthenticated: isAuthenticated,
      setAuthenticatedAccount: setAuthenticatedAccount,
      unauthenticate: unauthenticate,
      currentUser: ''
    };



    return Authentication;

    ////////////////////
    /**
    * @name register
    * @desc Try to register a new user
    * @param {string} username The username entered by the user
    * @param {string} password The password entered by the user
    * @param {string} email The email entered by the user
    * @returns {Promise}
    * @memberOf thinkster.authentication.services.Authentication
    */
    function register(email, password, username) {
      return $http.post('/api/v1/accounts/', {
        username: username,
        password: password,
        email: email,
      }).then(registerSuccessFn, registerErrorFn);

      function registerSuccessFn(data  ̶,̶ ̶s̶t̶a̶t̶u̶s̶,̶ ̶h̶e̶a̶d̶e̶r̶s̶,̶ ̶c̶o̶n̶f̶i̶g̶ ) {
        Authentication.login(email, password)
      };

      function registerErrorFn(data  ̶,̶ ̶s̶t̶a̶t̶u̶s̶,̶ ̶h̶e̶a̶d̶e̶r̶s̶,̶ ̶c̶o̶n̶f̶i̶g̶,) {
        console.log(data)
      }
    }

    function login(email, password){
      return $http.post('/api/v1/auth/login/', {
        email: email,
        password: password
      }).then(loginSuccessFn, loginErrorFn);

      function loginSuccessFn(data  ̶,̶ ̶s̶t̶a̶t̶u̶s̶,̶ ̶h̶e̶a̶d̶e̶r̶s̶,̶ ̶c̶o̶n̶f̶i̶g̶ ) {

        Authentication.setAuthenticatedAccount(data.data);
        // pass data through to dashboard controller
        window.location = '/dashboard'

        Authentication.currentUser += data.config.data.email
        console.log(Authentication.currentUser)

      }

      function loginErrorFn(data, status, headers, config) {
        console.error('Failed');
        console.log(data)
      }
}})();

dashboard.controller.js

//dashboard controller

(function() {
    'use strict';

    angular
      .module('thinkster.authentication.controllers')
      .controller('DashboardController', DashboardController);

    DashboardController.inject = ['$location, $scope', 'Authentication'];

    function DashboardController($location, $scope, Authentication){
        var vm = this;
        vm.user_info = user_info
        //vm.user_info2 = user_info

        function user_info() {
            return Authentication.currentUser
    }
    }

})();

I'm attempting to update currentUser in the loginSuccessFn(), it updates successfully in the service. I can see that in the console, but not when I try to reference it in my controller. Thanks in advance

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Amon
  • 2,725
  • 5
  • 30
  • 52
  • you mean give you my own plunker? – Amon Nov 22 '18 at 21:51
  • 1
    The dashboard controller immediately accesses `Authentification.currentUser` but the service only sets that variable *after* a response comes from the server. – georgeawg Nov 23 '18 at 00:59
  • Hey thanks for the reply, how would I fix that? – Amon Nov 23 '18 at 05:21
  • I wonder if I'd have to obtain `currentUser` from my login controller because that way I could have it return the value in a promise – Amon Nov 23 '18 at 05:36
  • The `.then` method returns an new promise that resolves to what is returned to the `.then` block. When a `.then` block omits a return statement, the function returns `undefined`. The derived promise will resolve as `undefined`. In addition a rejected promise will be **converted** to a fulfilled promise that resolves to `undefined`. – georgeawg Nov 23 '18 at 16:05
  • Okay, so are you saying I just return `currentUser` in my service? Or should I do what I suggested and instead return it in my login controller? – Amon Nov 23 '18 at 18:07
  • How does that other post answer my question? I read it and I'm still not sure – Amon Nov 23 '18 at 21:39
  • For anyone else in the future, it's easy I just added an if() statement to retrieve the value after I'm on a certain webpage in my controller. The value is retrieved from a function in the service that retrieves the user from the cookies. – Amon Dec 01 '18 at 18:02

1 Answers1

1

First why are you setting the variable using +=

(Authentication.currentUser += data.config.data.email)

Set the current user using =

  1. Authentication.currentUser = data.config.data

  2. Authentication.currentUser = data.config.data.email

Second user_info is a function so you have to call it, change

vm.user_info = user_info to vm.user_info = user_info()

Also I'd use camelCase for functions and under_score for object fields for readability and consistency

example

function userInfo() { // camelCase 
   return Authentication.currentUser;
}

var vm = this
vm.user_info = userInfo() // under_score object
andrewoodleyjr
  • 2,971
  • 2
  • 21
  • 21
  • Hey that isn't working either, I've tried both of those adjustments before. Is it because I'm retrieving `currentUser` before the request is made? – Amon Nov 23 '18 at 05:22
  • Yeah that must be the issue, because when I hardcode a value for `currentUser` it works – Amon Nov 23 '18 at 05:24
  • 1
    yea you have to set the value (finish the request and action) before retrieving the data point. – andrewoodleyjr Nov 23 '18 at 06:03
  • Okay how would I go about that? How do I retrieve the value after the request is made? with `.then()`? – Amon Nov 23 '18 at 22:08