authelia/client/src/containers/views/AuthenticationView/AuthenticationView.ts

48 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-01-20 02:10:43 +07:00
import { connect } from 'react-redux';
import QueryString from 'query-string';
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;
}
}
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;
let url: string | null = null;
if (ownProps.location) {
const params = QueryString.parse(ownProps.location.search);
if ('rd' in params) {
url = params['rd'] as string;
} else if (state.authentication.remoteState && state.authentication.remoteState.default_redirection_url) {
url = state.authentication.remoteState.default_redirection_url;
}
}
2019-01-20 02:10:43 +07:00
return {
redirectionUrl: url,
2019-01-20 02:10:43 +07:00
remoteState: state.authentication.remoteState,
stage: stage,
};
}
const mapDispatchToProps = (dispatch: Dispatch) => {
2019-01-20 02:10:43 +07:00
return {
onInit: async () => await FetchStateBehavior(dispatch)
2019-01-20 02:10:43 +07:00
}
}
export default connect(mapStateToProps, mapDispatchToProps)(AuthenticationView);