This repository has been archived by the owner on Apr 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
router.js
62 lines (58 loc) · 2.11 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { UserAuthWrapper } from 'redux-auth-wrapper'
import { LIST_PATH } from 'constants'
import LoadingSpinner from 'components/LoadingSpinner'
import {push } from 'react-router-redux'
const AUTHED_REDIRECT = 'AUTHED_REDIRECT'
const UNAUTHED_REDIRECT = 'UNAUTHED_REDIRECT'
/**
* @description Higher Order Component that redirects to `/login` instead
* rendering if user is not authenticated (default of redux-auth-wrapper).
* @param {Component} componentToWrap - Component to wrap
* @return {Component} wrappedComponent
*/
export const UserIsAuthenticated = UserAuthWrapper({
// eslint-disable-line new-cap
wrapperDisplayName: 'UserIsAuthenticated',
LoadingComponent: LoadingSpinner,
authSelector: ({ firebase: { auth } }) => auth,
authenticatingSelector: ({ firebase: { auth, isInitializing } }) =>
!auth.isLoaded || isInitializing,
predicate: auth => !auth.isEmpty,
redirectAction: newLoc => dispatch => {
dispatch(push(newLoc))
dispatch({
type: UNAUTHED_REDIRECT,
payload: { message: 'User is not authenticated.' }
})
}
})
/**
* @description Higher Order Component that redirects to listings page or most
* recent route instead rendering if user is not authenticated. This is useful
* routes that should not be displayed if a user is logged in, such as the
* login route.
* @param {Component} componentToWrap - Component to wrap
* @return {Component} wrappedComponent
*/
export const UserIsNotAuthenticated = UserAuthWrapper({
// eslint-disable-line new-cap
wrapperDisplayName: 'UserIsNotAuthenticated',
allowRedirectBack: false,
LoadingComponent: LoadingSpinner,
failureRedirectPath: (state, props) =>
// redirect to page user was on or to list path
LIST_PATH,
authSelector: ({ firebase: { auth } }) => auth,
authenticatingSelector: ({ firebase: { auth, isInitializing } }) =>
!auth.isLoaded || isInitializing,
// predicate: auth => auth === null,
predicate: auth => auth.isEmpty,
redirectAction: newLoc => dispatch => {
dispatch(push(newLoc))
dispatch({ type: AUTHED_REDIRECT })
}
})
export default {
UserIsAuthenticated,
UserIsNotAuthenticated
}