redux-token-auth/src/generate-require-signin-wrapper.tsx
2020-07-27 22:48:19 +09:00

61 lines
1.7 KiB
TypeScript

import * as React from 'react'
import { connect, ConnectedProps } from 'react-redux'
import {
GenerateRequireSignInWrapperConfig,
// ReduxState,
} from './types'
const generateRequireSignInWrapper = (
{ redirectPathIfNotSignedIn, mapStateToProps }: GenerateRequireSignInWrapperConfig
) => {
const requireSignInWrapper = (PageComponent: React.ComponentClass | React.FunctionComponent) => {
// const mapStateToProps = (state: ReduxState) => ({
// hasVerificationBeenAttempted: state.reduxTokenAuth.currentUser.hasVerificationBeenAttempted,
// isSignedIn: state.reduxTokenAuth.currentUser.isSignedIn
// })
const connector = connect(mapStateToProps)
type PropsFromRedux = ConnectedProps<typeof connector>
type Props = PropsFromRedux & {
readonly history: {
readonly replace: (path: string) => void
}
}
class GatedPage extends React.Component<Props> {
public componentDidUpdate() {
const {
history,
hasVerificationBeenAttempted,
isSignedIn,
} = this.props
if (hasVerificationBeenAttempted && !isSignedIn) {
history.replace(redirectPathIfNotSignedIn)
}
}
public render (): JSX.Element {
const {
hasVerificationBeenAttempted,
isSignedIn,
} = this.props
return (hasVerificationBeenAttempted && isSignedIn) ?
<PageComponent {...this.props} />
:
<div></div>;
}
}
return connect(
mapStateToProps,
)(GatedPage)
}
return requireSignInWrapper
}
export default generateRequireSignInWrapper
export type GenerateRequireSignInWrapper = typeof generateRequireSignInWrapper