Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/wwwroot/acceptanceTests/login.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,40 @@ describe('Login', () => {
expect(auth.isAuthed()).toBe(false);
});
});

describe('visiting /login with an existing session', () => {
it('should redirect an already-authenticated user to /reports and keep the session', () => {
// Arrange
var { $location, $scope, auth } = createContext();
auth.loginByToken('eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0ODQ2MzAzMTgsImV4cCI6MTQ4NzIyMjMxOCwiaWF0IjoxNDg0NjMwMzE4LCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjM0NzUxIiwic3ViIjoxMDAzLCJ1bmlxdWVfbmFtZSI6ImFtb3NjaGluaUBtYWtpbmdzZW5zZS5jb20iLCJyZWxheV9hY2NvdW50cyI6WyJhbW9zY2hpbmktbWFraW5nc2Vuc2UiXSwicmVsYXlfdG9rZW5fdmVyc2lvbiI6IjEuMC4wLWJldGE1In0.dQh20ukVSCP0rNXMWBh2DlPQXbP0uTaYzadRDNPXECI9lvCsgDKNXc2bToXAUQDeXw90kbHliVF-kCueW4gQLPBtMJOcHQFv6LfgspsG2jue2iMwoBC1q6UB_4xFlGoyhkRjldnQUV0oqBTzhFdXuTvQz53kRPiqILCHkd4FLl4KliBgdaDRwWz-HIjJwinMpnv_7V38CNvHlHo-q2XU0MnE3CsGXmWGoAgzN7rbeQPgI9azHXpbaUPh9n_4zjCydOSBC5tx7MtEAx3ivfFYImBPp2T2vUM-F5AwRh7hl_lMUvyQLal0S_spoT0XMGy8YhnjxXLoZeVRisWbxBmucQ');
expect(auth.isAuthed()).toBe(true);
expect(auth.isTemporarilyAuthed()).toBe(false);

// Act
$location.path('/login');
$scope.$apply();

// Assert
expect($location.path()).toBe('/reports');
expect(auth.isAuthed()).toBe(true);
expect(auth.getUserName()).toBe('amoschini@makingsense.com');
});

it('should clear the temporal session and stay on /login when the user is only temporally authed', () => {
// Arrange
var { $location, $scope, auth } = createContext();
auth.loginByToken('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYmYiOjE0ODQ2MjAxMjEsImV4cCI6MTUyODIwNDQzNCwiaWF0IjoxNDg0NjIwMTIxLCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjM0NzUxIiwidW5pcXVlX25hbWUiOiJhbW9zY2hpbmkrMUBtYWtpbmdzZW5zZS5jb20iLCJyZWxheV9vbl9wYXNzd29yZF9yZXNldCI6dHJ1ZSwicmVsYXlfdGVtcG9yYWxfdG9rZW4iOnRydWV9.gecKe6J6zQL7mHceq42fgjdpTUcVeQEBtSNp0mbI6Ig');
expect(auth.isAuthed()).toBe(true);
expect(auth.isTemporarilyAuthed()).toBe(true);

// Act
$location.path('/login');
$scope.$apply();

// Assert
expect($location.path()).toBe('/login');
expect(auth.isAuthed()).toBe(false);
expect(auth.isTemporarilyAuthed()).toBe(false);
});
});
});
26 changes: 22 additions & 4 deletions src/wwwroot/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,12 +254,16 @@
});

var _authReadyResolved = false;
auth.ready.finally(function () { _authReadyResolved = true; });
auth.ready.finally(function () {
_authReadyResolved = true;
if ($location.path() === '/login' && auth.isAuthed() && !auth.isTemporarilyAuthed()) {
$location.path(auth.getDefaultUrl() || '/reports');
}
});

$rootScope.$on('$locationChangeStart', function (event, next, current) {
var nextRelativeUrl = (next && next.split('#')[1]) || '/';
var forceLogoutUrls = [
'/login',
'/signup/registration',
'/signup/otp-validation',
'/signup/succeed',
Expand Down Expand Up @@ -340,7 +344,8 @@

function verifyAuthorization($location, auth) {
var openForAllUrls = ['/signup/error', '/temporal-token-error', '/dkim-configuration-tutorial'];
var requireLogoutUrls = ['/signup/confirmation', '/login', '/signup/registration', '/signup/otp-validation', '/signup/succeed', '/loginAdmin'];
var requireLogoutUrls = ['/signup/confirmation', '/signup/registration', '/signup/otp-validation', '/signup/succeed', '/loginAdmin'];
var redirectIfAuthedUrls = ['/login'];
var requireTemporalAuthUrls = ['/reset-password', '/change-email'];

// TODO: optimize it
Expand All @@ -349,8 +354,9 @@
var userIsAuthedTemporarily = userIsAuthed && auth.isTemporarilyAuthed();
var pageOpenForAll = openForAllUrls.includes(currentPath);
var pageRequireLogout = requireLogoutUrls.includes(currentPath);
var pageRedirectIfAuthed = redirectIfAuthedUrls.includes(currentPath);
var pageRequireTemporalAuth = requireTemporalAuthUrls.includes(currentPath);

if(!auth.isUrlAllowed(currentPath)) {
$location.path(auth.getDefaultUrl() || '/login');
}
Expand All @@ -366,6 +372,18 @@
return;
}

if (pageRedirectIfAuthed) {
if (userIsAuthedTemporarily) {
auth.logOut();
return;
}
if (userIsAuthed) {
$location.path(auth.getDefaultUrl() || '/reports');
return;
}
return;
}

if (pageRequireTemporalAuth && !userIsAuthedTemporarily) {
$location.path('/temporal-token-error');
return;
Expand Down
Loading