2019-01-20 02:10:43 +07:00
|
|
|
import { connect } from 'react-redux';
|
2019-01-31 04:44:03 +07:00
|
|
|
import QueryString from 'query-string';
|
2019-03-23 21:44:46 +07:00
|
|
|
import AuthenticationView, {StateProps, Stage, OwnProps} from '../../../views/AuthenticationView/AuthenticationView';
|
2019-01-20 02:10:43 +07:00
|
|
|
import { RootState } from '../../../reducers';
|
|
|
|
import { Dispatch } from 'redux';
|
|
|
|
import AuthenticationLevel from '../../../types/AuthenticationLevel';
|
|
|
|
import FetchStateBehavior from '../../../behaviors/FetchStateBehavior';
|
|
|
|
|
|
|
|
function authenticationLevelToStage(level: AuthenticationLevel): Stage {
|
|
|
|
switch (level) {
|
|
|
|
case AuthenticationLevel.NOT_AUTHENTICATED:
|
|
|
|
return Stage.FIRST_FACTOR;
|
|
|
|
case AuthenticationLevel.ONE_FACTOR:
|
|
|
|
return Stage.SECOND_FACTOR;
|
|
|
|
case AuthenticationLevel.TWO_FACTOR:
|
|
|
|
return Stage.ALREADY_AUTHENTICATED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-31 04:44:03 +07:00
|
|
|
const mapStateToProps = (state: RootState, ownProps: OwnProps): StateProps => {
|
2019-01-20 02:10:43 +07:00
|
|
|
const stage = (state.authentication.remoteState)
|
|
|
|
? authenticationLevelToStage(state.authentication.remoteState.authentication_level)
|
|
|
|
: Stage.FIRST_FACTOR;
|
2019-01-31 04:44:03 +07:00
|
|
|
|
|
|
|
let url: string | null = null;
|
|
|
|
if (ownProps.location) {
|
|
|
|
const params = QueryString.parse(ownProps.location.search);
|
|
|
|
if ('rd' in params) {
|
|
|
|
url = params['rd'] as string;
|
2019-04-25 04:52:08 +07:00
|
|
|
} else if (state.authentication.remoteState && state.authentication.remoteState.default_redirection_url) {
|
|
|
|
url = state.authentication.remoteState.default_redirection_url;
|
2019-01-31 04:44:03 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-20 02:10:43 +07:00
|
|
|
return {
|
2019-01-31 04:44:03 +07:00
|
|
|
redirectionUrl: url,
|
2019-01-20 02:10:43 +07:00
|
|
|
remoteState: state.authentication.remoteState,
|
|
|
|
stage: stage,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-02-13 05:23:43 +07:00
|
|
|
const mapDispatchToProps = (dispatch: Dispatch) => {
|
2019-01-20 02:10:43 +07:00
|
|
|
return {
|
2019-02-13 05:23:43 +07:00
|
|
|
onInit: async () => await FetchStateBehavior(dispatch)
|
2019-01-20 02:10:43 +07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(AuthenticationView);
|