Skip to content

netlifyIdentity.currentUser() always returns null unless in on event handler #355

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

Closed
SteveALee opened this issue Aug 21, 2020 · 2 comments
Closed

Comments

@SteveALee
Copy link
Contributor

SteveALee commented Aug 21, 2020

The only time I get a non null value back from netlifyIdentity.currentUser() is in an .on() handler when it is redundant as user is passed to the function anyway.

how do we get the current user info any other time? Other than doing a deep copy of the user object.

@jlengstorf
Copy link
Contributor

@SteveALee the widget needs to be instantiated before it can return anything, so .currentUser() can't be called before the init event has completed

it should work as expected in event handlers or other functions that are called after the page has loaded, though

if you need the user on page load, using .on('init') is probably the more straightforward approach

hopefully that helps — let me know if you have additional questions!

@SteveALee
Copy link
Contributor Author

SteveALee commented Aug 25, 2020

@jlengstorf thanks. Would you believe I just figured that in a VERY long thread with Jon Sully on the Netlify forums.

We were figuring out best place to put the token refresh() call. I'm using the global script in a plain paged site, he is using modules in SPA and I guess there are differences.

For your info here is my identity widget abstraction

if you need the user on page load, using .on('init') is probably the more straightforward approach

Yes, I spent a time exploring the various state permutations including browser refresh to understand when init and login are raised. All very sensible :)

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

No branches or pull requests

2 participants