@@ -107,12 +107,16 @@ Having cloud function contain logic about which users get assigned certain roles
107107const adminEmails = [
' [email protected] ' ]
// list of emails to automatically assign admin role to108108
109109async function assignUserRole (user ) {
110- const { uid , email , displayName } = user; // The email of the user.
110+ const { uid , email , displayName } = user // The email of the user.
111111 const newRole = adminEmails .includes (email) ? ' admin' : ' user'
112- await admin .firestore ().collection (' users' ).doc (uid).set ({ role: ' user' }, { merge: true })
112+ await admin
113+ .firestore ()
114+ .collection (' users' )
115+ .doc (uid)
116+ .set ({ role: ' user' }, { merge: true })
113117}
114118
115- exports .assignUserRole = functions .auth .user ().onCreate (assignUserRole);
119+ exports .assignUserRole = functions .auth .user ().onCreate (assignUserRole)
116120```
117121
118122More info is available about doing this in the [ extend auth with functions section of the firebase docs] ( https://firebase.google.com/docs/auth/extend-with-functions ) .
@@ -123,6 +127,8 @@ Using redux-auth-wrapper you can create higher order components that will make i
123127
124128Here is an example of an HOC that checks to make sure the user is an admin:
125129
130+ ** redux-auth-wrapper v1**
131+
126132``` js
127133import { get } from ' lodash' ;
128134import { UserAuthWrapper } from ' redux-auth-wrapper' ;
@@ -151,6 +157,34 @@ export const UserIsAdmin = UserAuthWrapper({ // eslint-disable-line new-cap
151157});
152158```
153159
160+ ** react-router v4 + redux-auth-wrapper v2**
161+
162+ ``` js
163+ import locationHelperBuilder from ' redux-auth-wrapper/history4/locationHelper'
164+ import { connectedRouterRedirect } from ' redux-auth-wrapper/history4/redirect'
165+ import createHistory from ' history/createBrowserHistory'
166+ import LoadingScreen from ' ../components/LoadingScreen' // change to your custom component
167+
168+ const locationHelper = locationHelperBuilder ({})
169+ const browserHistory = createHistory ()
170+
171+ export const UserIsAdmin = connectedRouterRedirect ({
172+ wrapperDisplayName: ' UserIsAuthenticated' ,
173+ LoadingComponent: LoadingScreen,
174+ allowRedirectBack: false ,
175+ redirectPath : (state , ownProps ) =>
176+ locationHelper .getRedirectQueryParam (ownProps) || ' /' ,
177+ authenticatingSelector : ({ firebase: { auth, profile, isInitializing } }) =>
178+ ! auth .isLoaded || ! profile .isLoaded || isInitializing === true ,
179+ authenticatedSelector : ({ firebase: { profile } }) =>
180+ profile .role .name === ' admin' ,
181+ redirectAction : newLoc => dispatch => {
182+ browserHistory .replace (newLoc)
183+ dispatch ({ type: UNAUTHED_REDIRECT })
184+ }
185+ })
186+ ```
187+
154188Here is an example of a UserHasPermission HOC that allows us to pass in a string permission (such as todos):
155189
156190_ Tip: you can place the below HOC in ` router.js ` together with ` UserIsNotAuthenticated ` and ` UserIsAuthenticated ` ._
@@ -188,30 +222,31 @@ export const UserHasPermission = permission => UserAuthWrapper({
188222** react-router v4 + redux-auth-wrapper v2**
189223
190224``` javascript
191- import locationHelperBuilder from ' redux-auth-wrapper/history4/locationHelper' ;
192- import { browserHistory } from ' react-router' ;
193- import LoadingScreen from ' ../components/LoadingScreen' ; // change it to your custom component
194-
195- const locationHelper = locationHelperBuilder ({});
196-
197- export const UserHasPermission = permission => UserAuthWrapper ({
198- wrapperDisplayName: ' UserHasPermission' ,
199- AuthenticatingComponent: LoadingScreen,
200- allowRedirectBack: false ,
201- redirectPath : (state , ownProps ) =>
202- locationHelper .getRedirectQueryParam (ownProps) || ' /login' ,
203- authenticatingSelector : ({ firebase: { auth, isInitializing } }) =>
204- ! auth .isLoaded || ! profile .isLoaded || isInitializing === true ,
205- authenticatedSelector : ({ firebase: { auth } }) =>
206- auth .isLoaded && ! auth .isEmpty ,
207- redirectAction : newLoc => (dispatch ) => {
208- browserHistory .replace (newLoc); // or routerActions.replace
209- dispatch ({ type: UNAUTHED_REDIRECT });
210- }
211- });
225+ import locationHelperBuilder from ' redux-auth-wrapper/history4/locationHelper'
226+ import { browserHistory } from ' react-router'
227+ import LoadingScreen from ' ../components/LoadingScreen' // change it to your custom component
228+ import { connectedRouterRedirect } from ' redux-auth-wrapper/history4/redirect'
229+
230+ const locationHelper = locationHelperBuilder ({})
231+
232+ export const UserHasPermission = permission =>
233+ connectedRouterRedirect ({
234+ wrapperDisplayName: ' UserHasPermission' ,
235+ AuthenticatingComponent: LoadingScreen,
236+ allowRedirectBack: false ,
237+ redirectPath : (state , ownProps ) =>
238+ locationHelper .getRedirectQueryParam (ownProps) || ' /login' ,
239+ authenticatingSelector : ({ firebase: { auth, isInitializing } }) =>
240+ ! auth .isLoaded || ! profile .isLoaded || isInitializing === true ,
241+ authenticatedSelector : ({ firebase: { auth } }) =>
242+ auth .isLoaded && ! auth .isEmpty ,
243+ redirectAction : newLoc => dispatch => {
244+ browserHistory .replace (newLoc) // or routerActions.replace
245+ dispatch ({ type: UNAUTHED_REDIRECT })
246+ }
247+ })
212248```
213249
214-
215250## Checking for roles on pages/components
216251
217252The Higher Order Component above can then be applied like so:
0 commit comments