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

Problem with the authUser object in withAuthorization/Authentication

JonatanLindstrom opened this issue · 2 comments

Using this code I have a problem in getting the authUser-object working correctly. The error thrown is: Unhandled Rejection (TypeError): Cannot read property 'roles' of undefined

When checking the condition in withAuthorization it first checks using an object authUser = {authUser: {userdata}} and then after some loading, checks again using an object authUser = {userdata}.

This means my conditions only pass if I write an extensive condition like:
const condition = authUser =>
authUser && ((authUser.roles && !!authUser.roles[ROLES.USER])
|| (authUser.authUser.roles && !!authUser.authUser.roles[ROLES.USER]));

This as I cannot be sure the .roles element exists without first checking the depth of the object.

I do the same merging of db and auth object as the example (even double checked with pasting the example code). Is there anyone else experiencing the same thing or know where I might have gone wrong?

Did you find a solution?

I'm stuck trying to read the name attribute from the user collection from the authUser listener.

Not really, I just did something similar to what I pasted with a check for the property at different levels within the object. This code for the condition is what was used which looks terrible, but does work.

const condition = authUser => authUser && ((authUser.roles && !!authUser.roles[ROLES.ADMIN]) || (authUser.authUser && authUser.authUser.roles && !!authUser.authUser.roles[ROLES.ADMIN]));

EDIT: Possibly this can be fixed with the spread operator, but as this was a quick school project I settled once I got it to work for my use case and didn't spend too much time with further investigations on how to make it properly.