session.js 674 B

12345678910111213141516171819202122
  1. /*
  2. The idea behind this service is that we start off the fetching of the session as soon
  3. as the service loads. Any subsequent requests for the session data are just returned
  4. the promise so redunant ajax calls are not made.
  5. */
  6. angular.module('NoteWrangler').factory('Session', function SessionFactory($http, $location) {
  7. var sessionPromise = $http({method: 'GET', url: "/session"});
  8. return {
  9. sessionData: function() {
  10. return sessionPromise;
  11. },
  12. authenticate: function() {
  13. this.sessionData().then(function(sessionUser){
  14. if(!sessionUser || !sessionUser.data.id) {
  15. $location.path('/');
  16. }
  17. });
  18. }
  19. };
  20. });