App.service('YammerSession', function () {
    this.create = function (token, userId) {
        this.token = token;
        this.userId = userId;
    };
    this.destroy = function () {
        this.token = null;
        this.userId = null;
    };
    return this;
});

App.service('Session', function (USER_ROLES) {
    this.create = function (token, userId, userRole) {
        this.token = token;
        this.userId = userId;
        this.userRole = userRole;
    };
    this.destroy = function () {
        this.token = null;
        this.userId = null;
        this.userRole = USER_ROLES.guest;
    };
    this.destroy();
    return this;
});

App.factory('AuthService', function ($rootScope, $http, $q, $timeout, Session, YammerSession, yammer,
                                     USER_ROLES, AUTH_EVENTS) {
    var authService = {};

    function updateYammerSession(yammerResponse) {
        if (yammerResponse.authResponse) {
            YammerSession.create(yammerResponse.access_token.token, yammerResponse.access_token.user_id);
        } else {
            YammerSession.destroy();
        }
    }

    function createStresstimeSession(stresstimeResponse) {
         Session.create(stresstimeResponse.token, stresstimeResponse.userId, USER_ROLES.admin);
    }

    yammer.getLoginStatus(function(response) {
        updateYammerSession(response);
        // TODO: check cookies to see if user is logged in our app
        if(YammerSession.token) {
            $rootScope.$apply(function() { // Necessary as it is an async response outside angular
                createStresstimeSession(YammerSession); // should be stresstime response (see todo above)
            });
        }
        $rootScope.$broadcast(AUTH_EVENTS.authServiceInitialized);

    });

    authService.login = function () {
        var deferred = $q.defer();
        if (!YammerSession.token) {
            yammer.login(function (response) {
                updateYammerSession(response);
                deferred.resolve(YammerSession);
            });
        }
        else {
            deferred.resolve(YammerSession);
        }
        return deferred.promise.then(function(yammerSession) {
           // TODO: log in our app
           createStresstimeSession(yammerSession); // should be stresstimeResponse
        });
    };

    authService.logout = function() {
        var deferred = $q.defer(); // this is done just to be compatible with the login function
        deferred.resolve();
        Session.destroy();
        return deferred.promise;
    }

    authService.isAuthenticated = function () {
        return !!Session.userId;
    };

    authService.isAuthorized = function (authorizedRoles) {
        if (!angular.isArray(authorizedRoles)) {
            authorizedRoles = [authorizedRoles];
        }
        return authorizedRoles.indexOf(Session.userRole) !== -1;
    };
    return authService;
});
@@ -639,24 +639,469 @@
  this;%0A%7D);%0A%0A
+App.factory('SessionStorage', %5B'$window', function($window) %7B%0A    var SessionStorage = %7B%7D;%0A%0A    SessionStorage.save = function(key, value) %7B%0A        $window.localStorage.setItem(key, JSON.stringify(value));%0A    %7D%0A    SessionStorage.load = function(key) %7B%0A        var value = $window.localStorage.getItem(key);%0A        if (value) %7B%0A            return JSON.parse(value);%0A        %7D%0A        return undefined;%0A    %7D%0A    return SessionStorage;%0A%0A%7D%5D);%0A%0A
 App.factory(
@@ -1250,16 +1250,32 @@
 H_EVENTS
+, SessionStorage
 ) %7B%0A    
@@ -1610,30 +1610,18 @@
 on(s
-tr
 ess
-timeResponse
+ion
 ) %7B%0A
- 
     
@@ -1644,75 +1644,138 @@
 te(s
-tr
 ess
-timeResponse.token, stresstimeResponse.userId, USER_ROLES.admi
+ion.token, session.userId, session.userRole); // TODO: change to session.userRole%0A        SessionStorage.save('session', Sessio
 n);%0A
@@ -1879,66 +1879,53 @@
     
-// TODO: check cookies to see if user is logged in our app
+var session = SessionStorage.load('session');
 %0A   
@@ -1943,32 +1943,43 @@
 merSession.token
+ && session
 ) %7B%0A            
@@ -2105,23 +2105,17 @@
 Session(
-YammerS
+s
 ession);
@@ -2714,16 +2714,86 @@
 our app%0A
+           yammerSession.userRole = USER_ROLES.admin; // mock for now%0A
         
@@ -3069,32 +3069,81 @@
 sion.destroy();%0A
+        SessionStorage.save(%22session%22, Session);%0A
         return d

Store session in window local storage