   # Keep track of previous and current states.
   $rootScope.$on '$stateChangeSuccess', (ev, to, toParams, from, fromParams) ->
     $rootScope.previousState =
-      name   : from.name
+      state  : from
       params : fromParams
 
     $rootScope.currentState =
-      name   : to.name
+      state  : to
       params : toParams
 
     $rootScope.nextState = undefined

    $state.go 'login'

  $rootScope.$on 'auth:already_logged', (msg, data) ->
    prev = $rootScope.previousState
    $state.go 'home'

  $rootScope.$on 'connection_refused', (msg, data) ->


  # State utils
  $rootScope.goBack = ->
    prev    = $rootScope.previousState
    current = $rootScope.currentState

    # If previous state is defined, then go to previous state
    if prev && prev.state && prev.state.name
      $state.go prev.state.name, prev.params

    # If not, deduce parent state from ncyBreadcrumb if that information is available
    else if current.state && current.state.ncyBreadcrumb && current.state.ncyBreadcrumb.parent
      $state.go current.state.ncyBreadcrumb.parent, current.params

    # Else, go to 'home'
    else
      $state.go 'home'

  $rootScope.goToNextState = ->
    next = $rootScope.nextState

    if next && next.state && next.state.name
      $state.go next.state.name, next.params
    else
      $state.go 'home'

