Skip to content

Commit

Permalink
Better handling when logging in and out
Browse files Browse the repository at this point in the history
- Stay on current page when logging in
- If access is denied, try to redirect back after login
- Stay on current page for log out, but reload
  • Loading branch information
rjmackay committed Nov 8, 2016
1 parent 145c7d2 commit 804a328
Showing 1 changed file with 8 additions and 4 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

0 comments on commit 804a328

Please sign in to comment.