I’ve got a strange issue where my auth seems to be working correctly on the front-end but all of my backend requireAuth() calls are returning auth in a “loading” state.
Here’s my auth context on the front-end:
And here’s the PrivatePageLoader component’s auth context:
Anyone know what’s wrong?
Actually I upgraded to 0.26.2 just now and everything seems to be working correctly! I’m still curious what the issue was though.
Hi @EvanAgee … I personally have not seen that before.
Can you confirm:
- Using Netlify Identity (and its standard widgets) and the versions installed
- Is this seen in Development or in Prod deployed to Netlify (or both)?
- Is there any chance the token has expired on the front end?
- In your api-side
auth.js
is there any info in
export const getCurrentUser = async (decoded, { _token, _type }) => {
return { ...decoded, roles: parseJWT({ decoded }).roles }
}
That sees if the token was decoded properly or not?
Thanks for reporting and hope we can figure this out together.
1 Like
Using Netlify Identity (and its standard widgets) and the versions installed
Is this seen in Development or in Prod deployed to Netlify (or both)?
This is happening on dev, haven’t deployed to prod yet
Is there any chance the token has expired on the front end?
No it doesn’t appear so.
In your api-side auth.js
is there any info in
I’ve tried a couple of different returns for getCurrentUser but currently I’m using
export const getCurrentUser = async ({ email }) => {
const user = await db.user.findUnique({ where: { email } })
return user
}
Thanks so much!
Is email
extracted from your decoded token here:
async ({ email }) => {
?
If you console log it just above const user
is it what you expect it to be?
@dthyresson yes the user is returned as expected which makes things even more confusing No matter what when I console context.currentUser it’s always empty.
Have you tried using the useAuth()
hook to fetch currentuser or userMetadata as per
API
The following values are available from the useAuth hook:
currentUser: An object containing information about the current user as set on the api side, or null if the user is not authenticated.
userMetadata: An object containing the user's metadata (or profile information) fetched directly from an instance of the auth provider client, or null if the user is not authenticated.
as as shown in the tutorial:
import { Link, routes } from '@redwoodjs/router'
import { useAuth } from '@redwoodjs/auth'
const BlogLayout = ({ children }) => {
const { logIn, logOut, isAuthenticated, currentUser } = useAuth()
return (
<div>
<h1>
<Link to={routes.home()}>Redwood Blog</Link>
</h1>
<nav>
<ul>
<li>
<Link to={routes.about()}>About</Link>
</li>
<li>
<Link to={routes.contact()}>Contact</Link>
</li>
<li>
<button onClick={isAuthenticated ? logOut : logIn}>
{isAuthenticated ? 'Log Out' : 'Log In'}
</button>
</li>
{isAuthenticated && <li>{currentUser.email}</li>}
</ul>
</nav>
<main>{children}</main>
</div>
)
}
export default BlogLayout
```
?
You may want `userMetadata` instead.
You can then set the result and `console.log(currentUser)` or `console.log(userMetadata)`.