Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions src/authWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const defaults = {
}

export default (args) => {
const { AuthenticatingComponent, FailureComponent, wrapperDisplayName } = {
const { AuthenticatingComponent, FailureComponent, wrapperDisplayName, LoadingComponent } = {
...defaults,
...args
}
Expand All @@ -19,11 +19,17 @@ export default (args) => {
const displayName = DecoratedComponent.displayName || DecoratedComponent.name || 'Component'

class UserAuthWrapper extends Component {
constructor() {
super();
this.state = { loading: true };
}

componentDidMount() {
if (this.props.preAuthAction) {
this.props.preAuthAction();
}
// reset the loading state once component is mounted.
this.setState({loading: false})
}

static displayName = `${wrapperDisplayName}(${displayName})`;
Expand All @@ -39,7 +45,20 @@ export default (args) => {

render() {
const { isAuthenticated, isAuthenticating } = this.props
if (isAuthenticated) {
if (this.state.loading) {
/**
* If loading component is not provided then render the authenticating component as a fallback, Since mostly
* authenticating component will be a loader or a spinner.
*/
return (
<React.Fragment>
{LoadingComponent
? <LoadingComponent />
: <AuthenticatingComponent {...this.props} />
}
</React.Fragment>
)
}else if (isAuthenticated) {
return <DecoratedComponent {...this.props} />
} else if (isAuthenticating) {
return <AuthenticatingComponent {...this.props} />
Expand Down