title: Retrieving authentication status and current user with ng-token-auth
body: <p>I'm using <a href="https://github.com/lynndylanhurley/ng-token-auth" rel="nofollow">ng-token-auth</a> along with <a href="https://github.com/lynndylanhurley/devise_token_auth" rel="nofollow">devise_token_auth</a> in my AngularJS application. I have implemented login and logout successfully, but I'm having trouble doing the following:</p>
  
  <ol>
  <li>Determining whether the user is authenticated, and</li>
  <li>retrieving the current user's info.</li>
  </ol>
  
  <p>I've partially solved 1 by using event subscribers:</p>
  
  <pre><code>myApp.run(['$rootScope',
    ($rootScope)-&gt;
      $rootScope.isAuthenticated = false
  
      $rootScope.$on('auth:validation-success', (e)-&gt;
        $rootScope.isAuthenticated = true
      )
      $rootScope.$on('auth:login-success', (e)-&gt;
        $rootScope.isAuthenticated = true
      )
  
      $rootScope.$on('auth:validation-error', (e)-&gt;
        $rootScope.isAuthenticated = false
      )
      $rootScope.$on('auth:invalid', (e)-&gt;
        $rootScope.isAuthenticated = false
      )
      $rootScope.$on('auth:login-error', (e)-&gt;
        $rootScope.isAuthenticated = false
      )
      $rootScope.$on('auth:logout-success', (e)-&gt;
        $rootScope.isAuthenticated = false
      )
      $rootScope.$on('auth:account-destroy-success', (e)-&gt;
        $rootScope.isAuthenticated = false
      )
      $rootScope.$on('auth:session-expired', (e)-&gt;
        $rootScope.isAuthenticated = false
      )
  ])
  </code></pre>
  
  <p>However, I am not sure this is the best way to do it. Maybe I should do it in a
  service instead?</p>
  
  <p>Regarding 2, I have installed <a href="https://docs.angularjs.org/api/ngResource/service/$resource" rel="nofollow">angular-resource</a> and configured a <code>User</code> resource, but I'm not really sure where to retrieve the user info (a service? a controller?) and how to update it when the user updates their profile (should I just listen to event subscribers or just reload the info on each state change?).</p>
  
  <p>I'm fairly new to Angular, so please let me know if I'm doing anything else wrong.</p>