Skip to content

add an async getUser function to Auth0ContextInterface#816

Closed
MisterSquishy wants to merge 2 commits intoauth0:mainfrom
MisterSquishy:main
Closed

add an async getUser function to Auth0ContextInterface#816
MisterSquishy wants to merge 2 commits intoauth0:mainfrom
MisterSquishy:main

Conversation

@MisterSquishy
Copy link

Description

the Auth0ContextInterface provides auth state and a bunch of async methods for updating that auth state. the user, however, is only populated in state as a side effect of other methods (namely getAccessTokenSilently, getAccessTokenWithPopup, and handleRedirectCallback) which means there is no way for a client to guarantee they have access when they first mount the hook. the standard way to deal with this is with an effect -- mount the hook, call getAccessTokenSilently or something, and rerender when the user is fetched. however, this isnt always possible, for instance tanstack router's beforeLoad method is a place where it would be really useful to await a user (to determine if they have access to this specific route). as of now, this library provides no way to do this, so i added this quick wrapper around the client.getUser method, for use in contexts where we cant rely on rerenders.

Testing

i relied on unit tests to test this, but theyre not particularly inspired -- given that this function just wraps client.getUser there really isnt much to test here.

  • This change adds test coverage for new/changed/fixed functionality

Checklist

  • I have added documentation for new/changed functionality in this PR or in auth0.com/docs
    • i didnt, but i can if it's deemed useful... i noticed that there are a handful of simple/self-explanatory methods that arent specifically called out in the examples, e.g. getAccessTokenWithPopup
  • All active GitHub checks for tests, formatting, and security are passing
  • The correct base branch is being used, if not the default branch

@MisterSquishy MisterSquishy requested a review from a team as a code owner November 23, 2024 16:04
@yogeshchoudhary147
Copy link
Contributor

Thanks for the PR and apologies for the delayed response. Are you still encountering this issue with TanStack Router beforeLoad hooks? If so, how did you work around it in the meantime? Understanding your current situation would help us prioritize this properly.

@luke-layerhealth
Copy link

Hey @yogeshchoudhary147 - the only workaround is to use a tool like patch-package to patch this functionality into auth0. This remains a problem for anyone who wants to access the current user in loaders using tanstack router which is a fairly common need.

I would consider this high priority given:

  • Issue has been open for >1 year
  • Resolving will greatly improve quality of life for users and provide much needed functionality
  • The surface area of this PR is very small so work needed to get this across the finish line is hopefully small as well

@yogeshchoudhary147
Copy link
Contributor

Closing this in favour of #1041 which has now been merged. The client prop on Auth0Provider allows you to share an Auth0Client instance between the React tree and router loaders — which covers the use case described here.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants