Invalid user context sent to function - react-netlify-identity

diet-planner.netlify.app

Hi I’m attempting to build a service making use of identity and functions. Sometimes the token sent to the function doesn’t appear to be valid, the context is missing a user context (context.clientContext.user).

I am using react-netlify-identity to help with Identity integration. I assume the token is expired and the AuthedFetch isn’t correctly updating the token it sends over.

Does anyone else have experience with this?

1 Like

Hi @AmarSibia, I’ve not used that package specifically but have you tried using the netlify-identity-widget directly? Also, react-netlify-identity is no longer maintained.

That said, do you have an example site where you can reproduce the issue? Perhaps that can give us a clue what the issue might be.

https://jotty.netlify.app/

I’ve been experiencing the same and started investigating the issue. After a period of inactivity (I guess the one hour the token is valid), I can only see the identity out of the context, the user object is not there. I think this goes beyond the AuthedFetch method but I’ve just started looking into it. Do you have any findings you would like to share?

As for using the sadly no longer officially maintained react-netlify-identity package, @Dennis it was the only way to go for me in order to achieve the degree of flexibility I wanted when building the authetication flow and with the limited skills I have to use the underlying gotrue-js library.

I’ve opened an issue here Authenticated API returns no user although being logged in · Issue #44 · netlify-labs/react-netlify-identity · GitHub seeking for suggestions.

I’m testing this solution at the moment, not sure if there’s a way of checking whether the token validity has expired or not other than comparing dates:

if (tokenExpires < nowDate) {
  load(getFreshJWT())
    .then((res) => res)
    .catch((err) =>  err);
}

The user object has a .jwt() function so user.jwt() that will return a new JWT. You could be sure this has the up to date information of that user. Does that help?

1 Like

Thanks for the message, really appreciate it. Do you suggest to run that function so that it can check the validity of the user token and return a fresh one anytime across the app at any rendering?
This is what I’m doing at the moment in the layout provider, but from what you’re saying I should call .jwt() at anytime with no need of checking whether the token has expired or not.

1 Like

This isn’t a “best practice” etc but personally, I call it before my code calls any functions for a user. When calling my own API for security I then check validity and roles etc. :slight_smile:

1 Like

Hi, @AaronP, that does help indeed, thank you so much and sorry for late reply but I wanted to test the solution I posted earlier for a little while.
It’s working just fine I think, I’ve created a react hook that checks for the token and the token validity and returns either a fresh token or logs out the user if the token object is null. In regards with this last check, I’ve come across a scenario in which although the user is logged in, the token object is null.

I’ve created a gist for what I’ve been testing successfully for about 10 days for https://jotty.netlify.app
Gist:
useUser-react-netlify-identity.js · GitHub

1 Like

Yep, that’s essentially what I was referring to :slight_smile: - Glad it helped!

1 Like