Skip to content

Commit 306006e

Browse files
authored
Merge pull request #401 from FromDoppler/DAT-2882
Dat 2882 - Redirect to reports when an auth user go to login page
2 parents 608d985 + b084e42 commit 306006e

2 files changed

Lines changed: 58 additions & 4 deletions

File tree

src/wwwroot/acceptanceTests/login.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,40 @@ describe('Login', () => {
111111
expect(auth.isAuthed()).toBe(false);
112112
});
113113
});
114+
115+
describe('visiting /login with an existing session', () => {
116+
it('should redirect an already-authenticated user to /reports and keep the session', () => {
117+
// Arrange
118+
var { $location, $scope, auth } = createContext();
119+
auth.loginByToken('eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0ODQ2MzAzMTgsImV4cCI6MTQ4NzIyMjMxOCwiaWF0IjoxNDg0NjMwMzE4LCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjM0NzUxIiwic3ViIjoxMDAzLCJ1bmlxdWVfbmFtZSI6ImFtb3NjaGluaUBtYWtpbmdzZW5zZS5jb20iLCJyZWxheV9hY2NvdW50cyI6WyJhbW9zY2hpbmktbWFraW5nc2Vuc2UiXSwicmVsYXlfdG9rZW5fdmVyc2lvbiI6IjEuMC4wLWJldGE1In0.dQh20ukVSCP0rNXMWBh2DlPQXbP0uTaYzadRDNPXECI9lvCsgDKNXc2bToXAUQDeXw90kbHliVF-kCueW4gQLPBtMJOcHQFv6LfgspsG2jue2iMwoBC1q6UB_4xFlGoyhkRjldnQUV0oqBTzhFdXuTvQz53kRPiqILCHkd4FLl4KliBgdaDRwWz-HIjJwinMpnv_7V38CNvHlHo-q2XU0MnE3CsGXmWGoAgzN7rbeQPgI9azHXpbaUPh9n_4zjCydOSBC5tx7MtEAx3ivfFYImBPp2T2vUM-F5AwRh7hl_lMUvyQLal0S_spoT0XMGy8YhnjxXLoZeVRisWbxBmucQ');
120+
expect(auth.isAuthed()).toBe(true);
121+
expect(auth.isTemporarilyAuthed()).toBe(false);
122+
123+
// Act
124+
$location.path('/login');
125+
$scope.$apply();
126+
127+
// Assert
128+
expect($location.path()).toBe('/reports');
129+
expect(auth.isAuthed()).toBe(true);
130+
expect(auth.getUserName()).toBe('amoschini@makingsense.com');
131+
});
132+
133+
it('should clear the temporal session and stay on /login when the user is only temporally authed', () => {
134+
// Arrange
135+
var { $location, $scope, auth } = createContext();
136+
auth.loginByToken('eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYmYiOjE0ODQ2MjAxMjEsImV4cCI6MTUyODIwNDQzNCwiaWF0IjoxNDg0NjIwMTIxLCJpc3MiOiJodHRwOi8vbG9jYWxob3N0OjM0NzUxIiwidW5pcXVlX25hbWUiOiJhbW9zY2hpbmkrMUBtYWtpbmdzZW5zZS5jb20iLCJyZWxheV9vbl9wYXNzd29yZF9yZXNldCI6dHJ1ZSwicmVsYXlfdGVtcG9yYWxfdG9rZW4iOnRydWV9.gecKe6J6zQL7mHceq42fgjdpTUcVeQEBtSNp0mbI6Ig');
137+
expect(auth.isAuthed()).toBe(true);
138+
expect(auth.isTemporarilyAuthed()).toBe(true);
139+
140+
// Act
141+
$location.path('/login');
142+
$scope.$apply();
143+
144+
// Assert
145+
expect($location.path()).toBe('/login');
146+
expect(auth.isAuthed()).toBe(false);
147+
expect(auth.isTemporarilyAuthed()).toBe(false);
148+
});
149+
});
114150
});

src/wwwroot/app.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,12 +254,16 @@
254254
});
255255

256256
var _authReadyResolved = false;
257-
auth.ready.finally(function () { _authReadyResolved = true; });
257+
auth.ready.finally(function () {
258+
_authReadyResolved = true;
259+
if ($location.path() === '/login' && auth.isAuthed() && !auth.isTemporarilyAuthed()) {
260+
$location.path(auth.getDefaultUrl() || '/reports');
261+
}
262+
});
258263

259264
$rootScope.$on('$locationChangeStart', function (event, next, current) {
260265
var nextRelativeUrl = (next && next.split('#')[1]) || '/';
261266
var forceLogoutUrls = [
262-
'/login',
263267
'/signup/registration',
264268
'/signup/otp-validation',
265269
'/signup/succeed',
@@ -340,7 +344,8 @@
340344

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

346351
// TODO: optimize it
@@ -349,8 +354,9 @@
349354
var userIsAuthedTemporarily = userIsAuthed && auth.isTemporarilyAuthed();
350355
var pageOpenForAll = openForAllUrls.includes(currentPath);
351356
var pageRequireLogout = requireLogoutUrls.includes(currentPath);
357+
var pageRedirectIfAuthed = redirectIfAuthedUrls.includes(currentPath);
352358
var pageRequireTemporalAuth = requireTemporalAuthUrls.includes(currentPath);
353-
359+
354360
if(!auth.isUrlAllowed(currentPath)) {
355361
$location.path(auth.getDefaultUrl() || '/login');
356362
}
@@ -366,6 +372,18 @@
366372
return;
367373
}
368374

375+
if (pageRedirectIfAuthed) {
376+
if (userIsAuthedTemporarily) {
377+
auth.logOut();
378+
return;
379+
}
380+
if (userIsAuthed) {
381+
$location.path(auth.getDefaultUrl() || '/reports');
382+
return;
383+
}
384+
return;
385+
}
386+
369387
if (pageRequireTemporalAuth && !userIsAuthedTemporarily) {
370388
$location.path('/temporal-token-error');
371389
return;

0 commit comments

Comments
 (0)