Skip to content

Commit

Permalink
Merge pull request #408 from ushahidi/hotfix/login-behaviour
Browse files Browse the repository at this point in the history
Better handling when logging in and out
  • Loading branch information
rjmackay authored Nov 8, 2016
2 parents 0c1b36c + 635c336 commit c95f021
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
12 changes: 8 additions & 4 deletions app/common/auth/authentication-events.run.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = AuthenticationEvents;

AuthenticationEvents.$inject = ['$rootScope', '$location', 'Authentication', 'Session', '_'];
function AuthenticationEvents($rootScope, $location, Authentication, Session, _) {
AuthenticationEvents.$inject = ['$rootScope', '$location', 'Authentication', 'Session', '_', '$route'];
function AuthenticationEvents($rootScope, $location, Authentication, Session, _, $route) {
$rootScope.currentUser = null;
$rootScope.loggedin = false;

Expand All @@ -23,6 +23,7 @@ function AuthenticationEvents($rootScope, $location, Authentication, Session, _)
if (redirect) {
$location.url(redirect);
}
$route.reload();
}

function doLogout(redirect) {
Expand All @@ -31,6 +32,7 @@ function AuthenticationEvents($rootScope, $location, Authentication, Session, _)
if (redirect) {
$location.url(redirect);
}
$route.reload();
}

// todo: move to service
Expand All @@ -54,11 +56,11 @@ function AuthenticationEvents($rootScope, $location, Authentication, Session, _)
};

$rootScope.$on('event:authentication:login:succeeded', function () {
doLogin(Session.getSessionDataEntry('loginPath') || '/');
doLogin(Session.getSessionDataEntry('loginPath'));
});

$rootScope.$on('event:authentication:logout:succeeded', function () {
doLogout('/');
doLogout();
});

// Don't think this is needed. We should already be logged out before this event
Expand All @@ -83,6 +85,8 @@ function AuthenticationEvents($rootScope, $location, Authentication, Session, _)
// We're logged out, redirect to login
if ($location.url() !== '/login') {
Session.setSessionDataEntry('loginPath', $location.url());
// We're logged in hit forbidden page
$location.url('/forbidden');
}
Authentication.openLogin();
}
Expand Down
18 changes: 12 additions & 6 deletions test/unit/common/auth/authentication-events.run.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ describe('global event handlers', function () {
mockedAuthenticationData,
mockedAuthenticationService,
$rootScope,
$location;
$location,
mockRoute = {
reload: jasmine.createSpy()
};

beforeEach(function () {

Expand Down Expand Up @@ -46,7 +49,10 @@ describe('global event handlers', function () {
.service('Authentication', function () {
return mockedAuthenticationService;
})
.run(require(rootPath + 'app/common/auth/authentication-events.run.js'));
.run(require(rootPath + 'app/common/auth/authentication-events.run.js'))
.service('$route', function () {
return mockRoute;
});

require(rootPath + 'test/unit/simple-test-app-config.js')(testApp);
});
Expand Down Expand Up @@ -92,8 +98,8 @@ describe('global event handlers', function () {
expect($rootScope.loggedin).toBe(true);
});

it('should change the path to "/"', function () {
expect($location.path()).toEqual('/');
it('should reload the route', function () {
expect(mockRoute.reload).toHaveBeenCalled();
});
});

Expand Down Expand Up @@ -129,8 +135,8 @@ describe('global event handlers', function () {
expect($rootScope.loggedin).toBe(false);
});

it('should change the path to "/"', function () {
expect($location.path()).toEqual('/');
it('should reload the route', function () {
expect(mockRoute.reload).toHaveBeenCalled();
});
});
});
Expand Down

0 comments on commit c95f021

Please sign in to comment.