react-firestore-authentication icon indicating copy to clipboard operation
react-firestore-authentication copied to clipboard

Problem with the authUser object in withAuthorization/Authentication

Open JonatanLindstrom opened this issue 5 years ago • 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?

JonatanLindstrom avatar Sep 05 '19 09:09 JonatanLindstrom

Did you find a solution?

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

MincePie avatar Jan 09 '20 21:01 MincePie

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.

JonatanLindstrom avatar Jan 11 '20 14:01 JonatanLindstrom