In my app, I want to create a service that looks in the backend to check if a user is logged in and returns the user data and privileges. My factory service looks like this:
app.factory 'Session', ['$http', ($http) ->
getSession: ->
$http.get("/admin/session")
]
Right now, I handle success and errors for the service in my controller shown below.
$scope.session = Session.getSession()
.success((data) ->
# Set the logged in user
$scope.admin = data
# Limit any privileges
)
.error((data) ->
# Logged in user not found so rediret to login screen
window.location = "/user/login"
)
My problem is that I need to repeat that same code in a lot of different controllers. Is there a better way to handle this? I am thinking I might need to do something with $rootScope but I do not understand how that works.
.error()in your service.$http.get("/admin/session")for each individual controller? Shouldn't you run this only once?