|
| 1 | +# Routing Recipes |
| 2 | + |
| 3 | +These recipes assume that you are using [`react-router`](https://github.com/ReactTraining/react-router), but the principles should be applicable to any routing solution. |
| 4 | + |
| 5 | +## Basic |
| 6 | + |
| 7 | +Routing can be changed based on data by using react lifecycle hooks such as `componentWillMount`, and `componentWillReceiveProps` to route users. This can be particularly useful when doing things such as route protection (only allowing user to view a route if they are logged in): |
| 8 | + |
| 9 | +```javascript |
| 10 | +import React, { Component, PropTypes } from 'react' |
| 11 | +import { connect } from 'react-redux' |
| 12 | +import { firebaseConnect, helpers } from 'react-redux-firebase' |
| 13 | + |
| 14 | +const { isLoaded, isEmpty, pathToJS } = helpers |
| 15 | + |
| 16 | +@firebaseConnect() |
| 17 | +@connect( |
| 18 | + ({ firebase }) => ({ |
| 19 | + auth: pathToJS(firebase, 'auth'), |
| 20 | + }) |
| 21 | +) |
| 22 | +export default class ProtectedPage extends Component { |
| 23 | + static propTypes = { |
| 24 | + auth: PropTypes.object, |
| 25 | + } |
| 26 | + |
| 27 | + componentWillReceiveProps({ auth }) { |
| 28 | + if (auth && !auth.uid) { |
| 29 | + this.context.router.push('/login') // redirect to /login if not authed |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + render() { |
| 34 | + return ( |
| 35 | + <div> |
| 36 | + You are authed! |
| 37 | + </div> |
| 38 | + ) |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## Advanced |
| 44 | + |
| 45 | +Using [`redux-auth-wrapper`](https://github.com/mjrussell/redux-auth-wrapper) you can easily create a Higher Order Component (wrapper) that can be used to redirect users based on Firebase state (including auth). |
| 46 | + |
| 47 | +### Auth Required ("Route Protection") |
| 48 | + |
| 49 | +In order to only allow authenticated users to view a page, a `UserIsAuthenticated` Higher Order Component can be created: |
| 50 | + |
| 51 | +```javascript |
| 52 | +import { browserHistory } from 'react-router' |
| 53 | +import { UserAuthWrapper } from 'redux-auth-wrapper' |
| 54 | +import { helpers } from 'react-redux-firebase' |
| 55 | +const { pathToJS } = helpers |
| 56 | + |
| 57 | +export const UserIsAuthenticated = UserAuthWrapper({ |
| 58 | + wrapperDisplayName: 'UserIsAuthenticated', |
| 59 | + authSelector: ({ firebase }) => pathToJS(firebase, 'auth'), |
| 60 | + authenticatingSelector: ({ firebase }) => pathToJS(firebase, 'isInitializing') === true, |
| 61 | + redirectAction: (newLoc) => (dispatch) => { |
| 62 | + browserHistory.replace(newLoc) |
| 63 | + dispatch({ |
| 64 | + type: 'UNAUTHED_REDIRECT', |
| 65 | + payload: { message: 'You must be authenticated.' }, |
| 66 | + }) |
| 67 | + }, |
| 68 | +}) |
| 69 | +``` |
| 70 | + |
| 71 | +Then it can be used as a Higher Order Component wrapper on a component: |
| 72 | + |
| 73 | +*es7 decorators* |
| 74 | + |
| 75 | +```javascript |
| 76 | +@UserIsAuthenticated // redirects to '/login' if user not is logged in |
| 77 | +export default class ProtectedThing extends Component { |
| 78 | + render() { |
| 79 | + return ( |
| 80 | + <div> |
| 81 | + You are authed! |
| 82 | + </div> |
| 83 | + ) |
| 84 | + } |
| 85 | +} |
| 86 | +``` |
| 87 | + |
| 88 | +*standard ES5/ES6* |
| 89 | + |
| 90 | +```javascript |
| 91 | +export default UserIsAuthenticated(ProtectedThing) |
| 92 | +``` |
| 93 | + |
| 94 | +Or it can be used at the route level: |
| 95 | + |
| 96 | +```javascript |
| 97 | +<Route path="/" component={App}> |
| 98 | + <Route path="login" component={Login}/> |
| 99 | + <Route path="foo" component={UserIsAuthenticated(Foo)}/> |
| 100 | +</Route> |
| 101 | +``` |
| 102 | + |
| 103 | + |
| 104 | +### Redirect Authenticated |
| 105 | +Just as easily as creating a wrapper for redirect if a user is not logged in, we can create one that redirects if a user *IS* authenticated. This can be useful for pages that you do not want a logged in user to see, such as the login page. |
| 106 | + |
| 107 | +```javascript |
| 108 | +import { browserHistory } from 'react-router' |
| 109 | +import { UserAuthWrapper } from 'redux-auth-wrapper' |
| 110 | +import { helpers } from 'react-redux-firebase' |
| 111 | +const { pathToJS } = helpers |
| 112 | + |
| 113 | +export const UserIsNotAuthenticated = UserAuthWrapper({ |
| 114 | + wrapperDisplayName: 'UserIsNotAuthenticated', |
| 115 | + allowRedirectBack: false, |
| 116 | + failureRedirectPath: '/', |
| 117 | + authSelector: ({ firebase }) => pathToJS(firebase, 'auth'), |
| 118 | + authenticatingSelector: ({ firebase }) => pathToJS(firebase, 'isInitializing') !== true, |
| 119 | + predicate: auth => auth === null, |
| 120 | + redirectAction: (newLoc) => (dispatch) => { |
| 121 | + browserHistory.replace(newLoc) |
| 122 | + dispatch({ |
| 123 | + type: 'AUTHED_REDIRECT', |
| 124 | + payload: { message: 'User is authenticated. Redirecting home...' } |
| 125 | + }) |
| 126 | + } |
| 127 | +}) |
| 128 | + |
| 129 | +``` |
| 130 | + |
| 131 | +Can then be used on a Login route component: |
| 132 | + |
| 133 | +```javascript |
| 134 | +import React, { Component } from 'react' |
| 135 | +import { firebaseConnect } from 'react-redux-firebase' |
| 136 | + |
| 137 | +@firebaseConnect() // adds this.props.firebase |
| 138 | +@UserIsNotAuthenticated // redirects to '/' if user is logged in |
| 139 | +export default class Login extends Component { |
| 140 | + googleLogin = () => { |
| 141 | + this.props.firebase.login({ provider: 'google' }) |
| 142 | + } |
| 143 | + render() { |
| 144 | + return ( |
| 145 | + <div> |
| 146 | + <button onClick={this.googleLogin}> |
| 147 | + Google Login |
| 148 | + </button> |
| 149 | + </div> |
| 150 | + ) |
| 151 | + } |
| 152 | +} |
| 153 | +``` |
0 commit comments