Skip to content

Commit

Permalink
Resolves #49 Fix bug in isAuthenticating check when props update
Browse files Browse the repository at this point in the history
  • Loading branch information
mjrussell committed Jun 27, 2016
1 parent b42a4d2 commit 53d4480
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
12 changes: 11 additions & 1 deletion src/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ export default function factory(React, empty) {
}

componentWillReceiveProps(nextProps) {
if(!nextProps.isAuthenticating && !isAuthorized(nextProps.authData) && isAuthorized(this.props.authData)) {
const willBeAuthorized = isAuthorized(nextProps.authData)
const willbeAuthenticating = nextProps.isAuthenticating
const wasAuthorized = isAuthorized(this.props.authData)
const wasAuthenticating = this.props.isAuthenticating

if ( // Redirect if:
// 1. Was authorized, but no longer and not current authenticating
(wasAuthorized && !willBeAuthorized && !willbeAuthenticating) ||
// 2. Was not authorized and authenticating but no longer authenticating
(wasAuthenticating && !willbeAuthenticating && !willBeAuthorized)
) {
createRedirect(nextProps.location, this.getRedirectFunc(nextProps))
}
}
Expand Down
37 changes: 31 additions & 6 deletions test/UserAuthWrapper-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,25 @@ import { UserAuthWrapper } from '../src'

const USER_LOGGED_IN = 'USER_LOGGED_IN'
const USER_LOGGED_OUT = 'USER_LOGGED_OUT'
const USER_LOGGING_IN = 'USER_LOGGING_IN'

const userReducerInitialState = {
userData: {},
isAuthenticating: false
}
const userReducer = (state = {}, { type, payload }) => {
if (type === USER_LOGGED_IN) {
return payload
}
if (type === USER_LOGGED_OUT) {
return {}
return {
userData: payload,
isAuthenticating: false
}
} else if (type === USER_LOGGED_OUT) {
return userReducerInitialState
} else if (type === USER_LOGGING_IN) {
return {
...state,
isAuthenticating: true
}
}
return state
}
Expand All @@ -28,10 +40,11 @@ const rootReducer = combineReducers({
user: userReducer
})

const userSelector = state => state.user
const userSelector = state => state.user.userData

const UserIsAuthenticated = UserAuthWrapper({
authSelector: userSelector,
authenticatingSelector: state => state.user.isAuthenticating,
redirectAction: routerActions.replace,
wrapperDisplayName: 'UserIsAuthenticated'
})
Expand Down Expand Up @@ -235,6 +248,18 @@ describe('UserAuthWrapper', () => {
expect(store.getState().routing.locationBeforeTransitions.pathname).to.equal('/login')
})

it('redirects if no longer authenticating', () => {
const { history, store } = setupTest()

store.dispatch({ type: USER_LOGGING_IN })

history.push('/auth')
expect(store.getState().routing.locationBeforeTransitions.pathname).to.equal('/auth')

store.dispatch({ type: USER_LOGGED_OUT })
expect(store.getState().routing.locationBeforeTransitions.pathname).to.equal('/login')
})

it('allows predicate authorization', () => {
const { history, store } = setupTest()

Expand Down Expand Up @@ -364,7 +389,7 @@ describe('UserAuthWrapper', () => {
const authSelector = (state, ownProps, isOnEnter) => {
if (!isOnEnter) {
return {
...state.user,
...userSelector(state),
...ownProps.routeParams // from React-Router
}
} else {
Expand Down

0 comments on commit 53d4480

Please sign in to comment.