I'm trying to pass the currently signed-in user to the resolve block of a 'user' state (I'm using ui-router), so upon sign-in, user will be in url of .../users/:id.
Other routes are nested inside :user route, so this seems the most logical path to me (I may be wrong).
I'm using angular_devise here so i should be able to extract the :id from from API via Auth, but I'm running into some trouble.
(Included other possible relevant files below.)
App.js
$stateProvder
.state('users', {
url: '/users/{id}',
templateUrl: 'home/_home.html',
controller: 'MainCtrl',
resolve: {
userPromise: ['$stateParams', 'users', 'Auth', function($stateParams, users, Auth) {
return users.get(
Auth.currentUser().then(function (user) {
return user.id;
})
);
}]
}
})
Users.js
.factory('users', ['$http', function($http) {
var u = {
userArray: []
};
u.get = function(id) {
return $http.get('/users/' + id + '.json').then(function(res) {
console.log('request: ', res.data);
angular.copy(res.data, u.userArray);
});
};
return u;
}]);
Users_controller.rb
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
authorize @user
respond_with @user
end
end
NavCtrl.js
.controller('NavCtrl', [
'$scope',
'Auth',
function($scope, Auth) {
$scope.signedIn = Auth.isAuthenticated;
$scope.logout = Auth.logout;
Auth.currentUser().then(function (user) {
$scope.user = user;
});
$scope.$on('devise:new-registration', function (e, user) {
$scope.user = user;
});
$scope.$on('devise:login', function (e, user) {
$scope.user = user;
});
$scope.$on('devise:logout', function (e, user) {
$scope.user = {};
});
}])
;
FYI. $http request is being sent to a rails backend.
I would be very grateful if anyone can advise what i might be doing wrong.