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

8
  • you mean give you my own plunker? Commented Nov 22, 2018 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. Commented Nov 23, 2018 at 0:59
  • Hey thanks for the reply, how would I fix that? Commented Nov 23, 2018 at 5: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 Commented Nov 23, 2018 at 5: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. Commented Nov 23, 2018 at 16:05

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

4 Comments

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?
Yeah that must be the issue, because when I hardcode a value for currentUser it works
yea you have to set the value (finish the request and action) before retrieving the data point.
Okay how would I go about that? How do I retrieve the value after the request is made? with .then()?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.