the-road-to-react-with-firebase / react-firebase-authentication

🔥 Boilerplate Project for Authentication with Firebase in React.
https://www.robinwieruch.de
1.02k stars 298 forks source link

Authorization permissions #13

Closed Wgil closed 5 years ago

Wgil commented 5 years ago

Hello,

I am building an application based on your boilerplate. Currently, I am facing an issue and I would appreciate if you could give me some insight on how could I approach it.

I need to know how could I pass down to condition in withAuthorization.js the user that I get from Firestore and not the auth user so I can authorize it based on other fields that are not available in the auth user.

I made these changes to the component so I can have a general UserContext instead of an AuthUserContext. Basically, after getting the authUser, I look for the Firestore user data and then I merge the objects. Doing so, I can't rely anymore on onAuthStateChanged hook in withAuthorization file. The problem is that my users are redirected to the sign in page until the UserContext is filled (with auth user id and Firestore user data) and then they get redirected to the home component instead of the page they requested. I would like to avoid that trip and make the users land to the route they type in.

const withAuthorization = (
  authCondition = permissions.isLoggedIn,
  getRedirectRoute = () => routes.SIGN_IN
) => Component => props => (
  <UserContext.Consumer>
    {user =>
      user && authCondition(user) ? (
        <Component {...props} user={user} />
      ) : (
        <Redirect to={getRedirectRoute(user)} />
      )
    }
  </UserContext.Consumer>
);

Any recommendation would be appreciated, thank you.

rwieruch commented 5 years ago

For anyone coming across this issue, have a look over here: https://www.robinwieruch.de/react-firebase-authorization-roles-permissions/

Sorry that I didn't answer this ealier @Wgil