Skip to content

Commit

Permalink
ZENKO-2673 Redirect user after successful authentication
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas2bert committed Jul 2, 2020
1 parent 97e6af2 commit ebbb826
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
25 changes: 25 additions & 0 deletions src/react/actions/__tests__/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
USER_MANAGER_ERROR_MSG,
errorUserManagerState,
initState,
signinRedirectCallbackState,
testActionFunction,
testDispatchFunction,
} from './utils/testUtil';
Expand Down Expand Up @@ -85,6 +86,30 @@ describe('auth actions', () => {
dispatchAction.LOCATION_PUSH_ACTION('/'),
],
},
{
it: 'signinCallback: should return push action with expected path "/data"',
fn: actions.signinCallback(),
storeState: signinRedirectCallbackState('/data'),
expectedActions: [
dispatchAction.LOCATION_PUSH_ACTION('/data'),
],
},
{
it: 'signinCallback: should return push action with expected path "/" if state path set to "/login"',
fn: actions.signinCallback(),
storeState: signinRedirectCallbackState('/login'),
expectedActions: [
dispatchAction.LOCATION_PUSH_ACTION('/'),
],
},
{
it: 'signinCallback: should return push action with expected path "/" if state path set to "/login/callback"',
fn: actions.signinCallback(),
storeState: signinRedirectCallbackState('/login/callback'),
expectedActions: [
dispatchAction.LOCATION_PUSH_ACTION('/'),
],
},
{
it: 'signinCallback: should handle error',
fn: actions.signinCallback(),
Expand Down
21 changes: 20 additions & 1 deletion src/react/actions/__tests__/utils/testUtil.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import { ApiErrorObject } from '../../../../js/mock/error';
import { ErrorUserManager } from '../../../../js/mock/userManager';
import { ErrorUserManager, MockUserManager } from '../../../../js/mock/userManager';
import configureStore from 'redux-mock-store';
import { initialFullState } from '../../../reducers/initialConstants';
import thunk from 'redux-thunk';
Expand Down Expand Up @@ -49,6 +49,25 @@ export function errorUserManagerState(): AppState {
};
}

export function signinRedirectCallbackState(path: string): AppState {
const state = initState;
const userManager = new MockUserManager();
userManager.signinRedirectCallback = () => {
return Promise.resolve({
state: {
path,
},
});
};
return {
...state,
auth: {
...state.auth,
userManager,
},
};
}

export function authenticatedUserState(): AppState {
const state = initState;
return {
Expand Down
5 changes: 4 additions & 1 deletion src/react/actions/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ export function signinCallback(): ThunkStatePromisedAction {
const userManager = getState().auth.userManager;
return userManager.signinRedirectCallback()
.then(user => {
const path = (user.state && user.state.path) || '/';
const path = user.state &&
user.state.path &&
user.state.path !== '/login' &&
user.state.path !== '/login/callback' ? user.state.path : '/';
dispatch(push(path));
})
.catch(error => {
Expand Down
2 changes: 1 addition & 1 deletion src/react/auth/Login.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { signin } from '../actions';

function Login(props) {
const { authenticated, dispatch, isSigningOut, location } = props;
const path = location && location.state && location.state.path && location.state.path !== '/login' ? props.location.state.path : '/';
const path = location && location.state && location.state.path || '/';
useEffect(() => {
if (!authenticated && !isSigningOut) {
dispatch(signin(path));
Expand Down

0 comments on commit ebbb826

Please sign in to comment.