Skip to content

Commit 486c13e

Browse files
committed
Merge pull request mjrussell#21 from mjrussell/ownProps
mjrussell#20 pass ownProps to the auth selector
2 parents ad15490 + 7f61bb3 commit 486c13e

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ Any time the user data changes, the UserAuthWrapper will re-check for authentica
104104

105105
#### Config Object Keys
106106

107-
* `authSelector(state, [ownProps]): authData` \(*Function*): A state selector for the auth data. Just like `mapToStateProps`
107+
* `authSelector(state, [ownProps], [isOnEnter]): authData` \(*Function*): A state selector for the auth data. Just like `mapToStateProps`.
108+
ownProps will be null if isOnEnter is true because onEnter hooks cannot receive the component properties. Can be ignored when not using onEnter.
108109
* `[failureRedirectPath]` \(*String*): Optional path to redirect the browser to on a failed check. Defaults to `/login`
109110
* `[redirectAction]` \(*Function*): Optional redux action creator for redirecting the user. If not present, will use React-Router's router context to perform the transition.
110111
* `[wrapperDisplayName]` \(*String*): Optional name describing this authentication or authorization check.

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const UserAuthWrapper = (args) => {
4848
}
4949

5050
@connect(
51-
state => { return { authData: authSelector(state) } },
51+
(state, ownProps) => { return { authData: authSelector(state, ownProps, false) } },
5252
mapDispatchToProps,
5353
)
5454
class UserAuthWrapper extends Component {
@@ -108,7 +108,7 @@ const UserAuthWrapper = (args) => {
108108
}
109109

110110
wrapComponent.onEnter = (store, nextState, replace) => {
111-
const authData = authSelector(store.getState())
111+
const authData = authSelector(store.getState(), null, true)
112112
ensureAuth({ location: nextState.location, authData }, replace)
113113
}
114114

test/UserAuthWrapper-test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,46 @@ describe('UserAuthWrapper', () => {
320320
expect(store.getState().routing.location.pathname).to.equal('/login')
321321
expect(store.getState().routing.location.search).to.equal('?redirect=%2FonEnter')
322322
})
323+
324+
it('passes ownProps for auth selector', () => {
325+
const authSelector = (state, ownProps, isOnEnter) => {
326+
if (!isOnEnter) {
327+
return {
328+
...state.user,
329+
...ownProps.routeParams // from React-Router
330+
}
331+
} else {
332+
return {}
333+
}
334+
}
335+
336+
const UserIsAuthenticatedProps = UserAuthWrapper({
337+
authSelector: authSelector,
338+
redirectAction: routeActions.replace,
339+
wrapperDisplayName: 'UserIsAuthenticatedProps',
340+
predicate: user => user.firstName === 'Test' && user.id === '1'
341+
})
342+
343+
const routes = (
344+
<Route path="/" component={App} >
345+
<Route path="login" component={UnprotectedComponent} />
346+
<Route path="ownProps/:id" component={UserIsAuthenticatedProps(UnprotectedComponent)} />
347+
</Route>
348+
)
349+
350+
const { history, store } = setupTest(routes)
351+
352+
expect(store.getState().routing.location.pathname).to.equal('/')
353+
expect(store.getState().routing.location.search).to.equal('')
354+
355+
store.dispatch(userLoggedIn())
356+
357+
history.push('/ownProps/1')
358+
expect(store.getState().routing.location.pathname).to.equal('/ownProps/1')
359+
expect(store.getState().routing.location.search).to.equal('')
360+
361+
history.push('/ownProps/2')
362+
expect(store.getState().routing.location.pathname).to.equal('/login')
363+
expect(store.getState().routing.location.search).to.equal('?redirect=%2FownProps%2F2')
364+
})
323365
})

0 commit comments

Comments
 (0)