diff --git a/frontend/projects/upgrade/src/app/core/auth/auth.service.ts b/frontend/projects/upgrade/src/app/core/auth/auth.service.ts index ac821932fc..3aa50a374d 100644 --- a/frontend/projects/upgrade/src/app/core/auth/auth.service.ts +++ b/frontend/projects/upgrade/src/app/core/auth/auth.service.ts @@ -36,6 +36,8 @@ export class AuthService { initializeUserSession(): void { const currentUser = this.getUserFromBrowserStorage(); + this.determinePostLoginDestinationUrl(); + if (currentUser) { this.handleAutomaticLogin(currentUser); } else { @@ -43,6 +45,19 @@ export class AuthService { } } + /** + * determinePostLoginDestinationUrl + * + * - navigate to /home if user started from login or if no path is present + * - otherwise, navigate to the path they started from after google login does its thing + */ + + determinePostLoginDestinationUrl(): void { + const originalDestinationUrl = window.location.pathname?.endsWith('login') ? 'home' : window.location.pathname; + + this.setRedirectionUrl(originalDestinationUrl); + } + initializeGoogleSignInButton(btnRef: ElementRef): void { this.initializeGoogleSignIn(); this.renderGoogleSignInButton(btnRef); diff --git a/frontend/projects/upgrade/src/app/core/auth/store/auth.models.ts b/frontend/projects/upgrade/src/app/core/auth/store/auth.models.ts index ae6e437933..73558ed96c 100755 --- a/frontend/projects/upgrade/src/app/core/auth/store/auth.models.ts +++ b/frontend/projects/upgrade/src/app/core/auth/store/auth.models.ts @@ -21,7 +21,7 @@ export interface UserPermission { export interface AuthState { isLoggedIn: boolean; isAuthenticating: boolean; - redirectUrl?: string; + redirectUrl: string; user: User; googleCredential: string; } diff --git a/frontend/projects/upgrade/src/app/core/auth/store/auth.reducer.ts b/frontend/projects/upgrade/src/app/core/auth/store/auth.reducer.ts index 331304dc43..0cbfcbc189 100755 --- a/frontend/projects/upgrade/src/app/core/auth/store/auth.reducer.ts +++ b/frontend/projects/upgrade/src/app/core/auth/store/auth.reducer.ts @@ -7,6 +7,7 @@ export const initialState: AuthState = { isAuthenticating: false, user: null, googleCredential: null, + redirectUrl: '/home', }; const reducer = createReducer( diff --git a/frontend/projects/upgrade/src/app/core/meta-reducers/clear-state.reducer.spec.ts b/frontend/projects/upgrade/src/app/core/meta-reducers/clear-state.reducer.spec.ts index 4e1ce6f016..a702c37142 100644 --- a/frontend/projects/upgrade/src/app/core/meta-reducers/clear-state.reducer.spec.ts +++ b/frontend/projects/upgrade/src/app/core/meta-reducers/clear-state.reducer.spec.ts @@ -1,5 +1,5 @@ import { createReducer } from '@ngrx/store'; -import { actionLogoutSuccess } from '../auth/store/auth.actions'; +import { actionLogoutSuccess, actionLoginStart } from '../auth/store/auth.actions'; import { LocalStorageService } from '../local-storage/local-storage.service'; import { ThemeOptions } from '../settings/store/settings.model'; import { clearState } from './clear-state.reducer'; @@ -10,13 +10,16 @@ describe('clearState', () => { settings: { theme: ThemeOptions.DARK_THEME, }, + auth: { + redirectUrl: '/test', + }, }; beforeEach(() => { LocalStorageService.prototype.removeItem = jest.fn(); }); - it('should reset settings state and clear localStorage settings', () => { + it('should reset settings and auth state except for theme and redirectUrl on actionLogoutSuccess, and also clear localStorage settings', () => { const reducer = createReducer(mockState); const metaReducer = clearState(reducer); const expectedResetState = { @@ -25,6 +28,13 @@ describe('clearState', () => { toCheckAuth: null, toFilterMetric: null, }, + auth: { + isLoggedIn: false, + isAuthenticating: false, + user: null, + googleCredential: null, + redirectUrl: '/test', + }, }; const newState = metaReducer(mockState, actionLogoutSuccess()); @@ -34,4 +44,37 @@ describe('clearState', () => { ); expect(newState).toEqual(expectedResetState); }); + + it('should NOT reset anything or clear localStorage if action is anything but actionLogoutSuccess', () => { + const mockWithNonDefaultState = { + ...mockState, + auth: { + ...mockState.auth, + isLoggedIn: true, + }, + }; + const reducer = createReducer(mockState); + const metaReducer = clearState(reducer); + const expectedResetState = { + settings: { + theme: ThemeOptions.DARK_THEME, + toCheckAuth: null, + toFilterMetric: null, + }, + auth: { + isLoggedIn: true, + isAuthenticating: false, + user: null, + googleCredential: null, + redirectUrl: '/test', + }, + }; + + const newState = metaReducer(mockWithNonDefaultState, actionLoginStart()); + + expect(LocalStorageService.prototype.removeItem).not.toHaveBeenCalledTimes( + Object.keys(ExperimentLocalStorageKeys).length + ); + expect(newState).not.toEqual(expectedResetState); + }); }); diff --git a/frontend/projects/upgrade/src/app/core/meta-reducers/clear-state.reducer.ts b/frontend/projects/upgrade/src/app/core/meta-reducers/clear-state.reducer.ts index 94f282fe1e..c52026cefb 100644 --- a/frontend/projects/upgrade/src/app/core/meta-reducers/clear-state.reducer.ts +++ b/frontend/projects/upgrade/src/app/core/meta-reducers/clear-state.reducer.ts @@ -3,6 +3,7 @@ import * as AuthActions from '../auth/store/auth.actions'; import { ExperimentLocalStorageKeys } from '../experiments/store/experiments.model'; import { LocalStorageService } from '../local-storage/local-storage.service'; import { SettingsState } from '../settings/store/settings.model'; +import { AuthState } from '../auth/store/auth.models'; export function clearState(reducer) { return (state, action: Action) => { @@ -12,10 +13,18 @@ export function clearState(reducer) { toCheckAuth: null, toFilterMetric: null, }; + const authState: AuthState = { + isLoggedIn: false, + isAuthenticating: false, + user: null, + googleCredential: null, + redirectUrl: state.auth.redirectUrl, + }; const localStorageService = new LocalStorageService(); state = { settings: settingState, // Used to persist theme, + auth: authState, // Used to persist redirectUrl }; Object.values(ExperimentLocalStorageKeys).forEach((key) => {