FusionAuth / fusionauth-react-sdk

An SDK for using FusionAuth with React
https://fusionauth.io/docs/quickstarts/#single-page-app
Apache License 2.0
9 stars 5 forks source link

User object from the SDK is not set #22

Closed mooreds closed 5 months ago

mooreds commented 1 year ago

The issue I am seeing is that the user object from the sdk is just an empty object. The “isAuthenticated” property is being correctly populated though.

    const { user, isAuthenticated,  } = useFusionAuth();

This results in user being set to an empty object: {} and isAuthenticated being true.

I am able to get the user by just reading the cookie myself, but it seems like user should be set.

It looks like setUser is configured here: https://github.com/FusionAuth/fusionauth-react-sdk/blob/main/src/providers/FusionAuthProvider.tsx#L56

and it seems like useEffect should be setting the user object here: https://github.com/FusionAuth/fusionauth-react-sdk/blob/main/src/providers/FusionAuthProvider.tsx#L127 but the useFusionAuth method doesn't return a valid user.

joshdura commented 1 year ago

Not sure if this is part of the same issue or not, but will post this here until confirmed otherwise. When I login for the first time (when the access_token cookie doesn't exist and the POST /token-exchange endpoint is called), the user cookie is correctly populated. However, when I logout and then log back in, the access_token cookie remains the same, and the user cookie is set to the string undefined.

mooreds commented 1 year ago

Hmm, seems to me that the access_token needs to be removed on logout.

joshdura commented 1 year ago

Hmm, seems to me that the access_token needs to be removed on logout.

Yeah, that's what I was thinking as well. Is that something that should be handled on my side? Or something that the SDK should be taking care of?

mooreds commented 1 year ago

I think the SDK should handle that. I think we can get that fixed in the next few days.

mooreds commented 1 year ago

I'm going to track the access cookie remaining in a separate issue: https://github.com/FusionAuth/fusionauth-react-sdk/issues/27

mooreds commented 1 year ago

Okay, heard a bit from one of the devs who wrote this. They pointed me to https://github.com/FusionAuth/fusionauth-example-react-sdk/blob/main/server/routes/token-exchange.js#L35 and said that for some reason it isn't being populated. Need to do some more research.

JakeLo123 commented 5 months ago

Looking at the provider code, there are some instances where user and isAuthenticated are not set at the same time. There are cases where they may be out of sync. We can adjust the code to handle these and add tests to watch for future breakage.

This PR inadvertently addresses some of it — we can address the rest once it goes in.