Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

netlify identity tokens need to be refreshed #1833

Closed
benatkin opened this issue Feb 22, 2021 · 8 comments
Closed

netlify identity tokens need to be refreshed #1833

benatkin opened this issue Feb 22, 2021 · 8 comments
Assignees

Comments

@benatkin
Copy link

I'm discussing on the forum. I've been debugging it long enough that I think the issue exists. I should have complete answers soon, but I thought I would start an issue now.

I've checked several times that I'm using Netlify Identity Widget with Redwood according to the docs, yet after a delay I keep finding myself partially or completely logged out. I just tracked it down to an expired token.

I think this is partly an upstream issue, but I don't think netlifyIdentity.currentUser() returns a promise, so await netlifyIdentity.currentUser() isn't sufficient to get a fresh token. I think await netlifyIdentity.currentUser?.jwt?() is.

Sorry for the partial message. I'm working on fixing the issue my project and will provide more info as I get it.

@benatkin
Copy link
Author

currentUser in netlify-identity-widget calls currentUser which calls User.recoverSession and none appear to return promises

@benatkin
Copy link
Author

@benatkin
Copy link
Author

I was able to fix it in my app by overriding currentUser in the client passed to AuthProvider and making it await on jwt() before the promise:

import netlifyIdentity from 'netlify-identity-widget'

const currentUser = async () => {
  const currentUser = netlifyIdentity.currentUser()
  if (typeof currentUser?.jwt === 'function') {
    console.log('awaiting currentUser.jwt() before returning currentUser')
    await currentUser.jwt()
  }
  return currentUser
}

const netlifyIdentityWrapped = new Proxy(netlifyIdentity, {
  get(target, name) {
    return name === 'currentUser' ? currentUser : target[name]
  },
})

ReactDOM.render(
  <FatalErrorBoundary page={FatalErrorPage}>
    <AuthProvider client={netlifyIdentityWrapped} type="netlify">
      <RedwoodApolloProvider>
        <Routes />
      </RedwoodApolloProvider>
    </AuthProvider>
  </FatalErrorBoundary>,
  document.getElementById('redwood-app')
)

@dthyresson
Copy link
Contributor

dthyresson commented Feb 24, 2021

@benatkin
Do you think your example

 const currentUser = netlifyIdentity.currentUser()
  if (typeof currentUser?.jwt === 'function') {
    console.log('awaiting currentUser.jwt() before returning currentUser')
    await currentUser.jwt()
  }
  return currentUser

which I saw similar code for in netlify/gotrue-js#71 (comment)

const user = this.netlifyIdentity.currentUser();
if (user) {
	console.log('token', user.token)
	const jwt = user.jwt(true);
	jwt.then((response) => {
		console.log("User token refreshed.", response);
		const newUser = this.netlifyIdentity.currentUser()
		console.log('new-token', newUser.token)
	});
}

should be part of the

    restoreAuthState: async () => {
      return client.currentUser()
    },

for the netlify AuthProvider?

restoreAuthState: async () => {

@benatkin
Copy link
Author

Yeah, it seems like with my fix there could be a delay.

Would the restoreAuth solution work after having the site open for over an hour, which is how long before tokens need to be refreshed?

If not, perhaps calling jwt() in both restoreAuth and getToken is needed.

My proxy patch is working all the time except it glitched once. I can try returning a proxied currentUser in my proxy which proxies token to a proxy which proxies accessToken to the value obtained from jwt() and see if that fixes it more.

@benatkin
Copy link
Author

It's on https://app.resources.co/ by the way. Anyone can sign in with GitHub.

@dthyresson
Copy link
Contributor

@benatkin There have been some improvement made to token refreshing sinch this issue came up.

Can we confirm it is still an issue in v0.33.2+?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants