currentUser is null as implemented in RedwoodJS Tutorial, Part 4

Just a heads up …
After completing all 4 video tutorials, it appears useAuth is not returning any value for currentUser. I did find I could retrieve email from userMetadata however. Just thought you should know other may too have issues with this.

const { userMetadata } = useAuth()

Came here with this question in mind, thanks!

Our apologies, this was renamed in v0.11.0:

@rob Maybe we can provide some kind of popup during the video tutorial?

Yeah, that’s a good idea, but I’m wondering why it returns null since it should return the decoded jwt if you’re using auth0 and netlify.

Okay so prior to 0.11, your definition of getCurrentUser in api/src/lib/auth.js was only used on the api side. On the web side, in the case of Netlify Identity, currentUser was what was returned by the netlify-identity widget.

Now, post 0.11 what’s returned by getCurrentUser is used on both the api and web side, and userMetadata falls back to the contents of the JWT (which is returned by default in getCurrentUser)

So in 0.11+, without making any changes to auth.js currentUser should return the content of the JWT on the web side, right @peterp? Maybe if the user isn’t logged in at all it returns null?

Upgrading my completed Tutorial blog from v0.10 to v0.11 worked just fine, including retrieving the email on BlogLayout.js with currentUser.email from const { logIn, logOut, isAuthenticated, currentUser } = useAuth().

The only code I have a question about is this snippet from auth.js:

export const requireAuth = () => {
  if (!context.currentUser) {
    throw new AuthenticationError()
  }
}

Yup, that’s exactly how it should work.

:slight_smile: What’s the question that you have about it?

ha! So. Many. And all very mysterious…

(sorry for lack of clarity)

Question --> per Auth changes in v0.11.0, is this still the correct implementation, i.e. context.currentUser?

1 Like

That still works for me in Conway—I have requireAuth() calls in my services so that non-logged in users can’t do anything. context.currentUser is how you get currentUser on the api side, on the web side you can just reference currentUser from useAuth().

1 Like

@rsturim @jerryasher Could either of you share some code with me so that I can try to reproduce this?

@peterp

Well, this is from the tutorial, and this is what I have that works…

redwoodblog > web > src > layouts > BlogLayout > BlogLayout.js

import { Link, routes } from '@redwoodjs/router'
import { useAuth } from '@redwoodjs/auth'

const BlogLayout = ({ children }) => {
  const { logIn, logOut, isAuthenticated } = useAuth()
  const { userMetadata } = useAuth()
  return (
    <div border="5px solid black">
      <h1>
        <Link to={routes.home()}>Redwood Blog</Link>
      </h1>
      <nav>
        <ul>
          <li>
            <Link to={routes.about()}>About</Link>
          </li>
          <li>
            <a href="#" onClick={isAuthenticated ? logOut : logIn}>
              {isAuthenticated ? 'Log Out' : 'Log In'}
            </a>
          </li>
          {isAuthenticated && <li>{userMetadata.email}</li>}
        </ul>
      </nav>
      <main>{children}</main>
    </div>
  )
}

export default BlogLayout

So presumably change that userMetaData back to currentUser for great lossage.

@jerryasher Are you using the Netlify auth provider? And could you share you api/src/lib/auth.js file?

@peterp api/src/lib/auth.js

// Define what you want `currentUser` to return throughout your app. For example,
// to return a real user from your database, you could do something like:
//
//   export const getCurrentUser = async ({ email }) => {
//     return await db.user.findOne({ where: { email } })
//   }

import { AuthenticationError } from '@redwoodjs/api'

export const getCurrentUser = async (jwt) => {
  return jwt
}

// Use this function in your services to check that a user is logged in, and
// optionally raise an error if they're not.

export const requireAuth = () => {
  if (!context.currentUser) {
    throw new AuthenticationError("You don't have permission to do that.")
  }
}