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.
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()
}
}
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().
// 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.")
}
}