Now, I have just removed the @redwoodjs/auth package. So the import has to change. I looked a bit in the code and found that I can export a thing called AuthContext from the createAuth() function. So in my web/src/auth.ts I just destructed it from the function and am changing the import to src/auth.
However, <AuthContext.Provider /> does not seem to work properly in storybook, since I get an error in storybook with:
Cannot read properties of undefined (reading 'Provider')
TypeError: Cannot read properties of undefined (reading 'Provider')
at LoggedInDecorator (http://localhost:7910/main.iframe.bundle.js:50932:119)...
I’m seeing slightly different behavior from mockCurrentUser, which doesn’t seem to reset anymore?
I have a test file, that has four tests. Two logged in, two not logged in.
After running the logged in ones, I now need to call mockCurrentUser(null) before running the non logged in ones. I guess I could just revert the order, but I’m unsure if that change was intended. (probably an upstream change)
@AlexRoosWork definitely not an edge case, an oversight on my part. We made complimentary code changes to mocking auth in tests and Storybook, so I should’ve highlighted this.
One thing that concerns me a bit about your code example is that you import from /dist, but I think my suggestion below still applies.
Based on what I can infer from the code changes, I think there’s good news, in that the new way of mocking auth in web-side tests and in Storybook is simpler. Mainly, you don’t need a LoggedInDecorator of sorts anymore. Instead, you just have to mock the current user via mockCurrentUser, and useAuth will more or less simply return what you mock.
Say in your home page, you import the useAuth hook from 'src/auth' and use its props to conditionally render like so:
To see the “You are authenticated” state, you’re going to have to mock the current user in the story:
import type { ComponentMeta } from '@storybook/react'
import HomePage from './HomePage'
export const generated = () => {
// You need to mock the current user here; and this function is globally available in stories
mockCurrentUser({ name: 'jtoar' })
return <HomePage />
}
export default {
title: 'Pages/HomePage',
component: HomePage,
} as ComponentMeta<typeof HomePage>
I’ll try to follow up with a more comprehensive example, and will let @Tobbe chime in cause I could’ve gotten some details wrong, but maybe give that a shot @AlexRoosWork and let me know how it goes?
@dennemark yes this is for the api side. And between those two packages, I’m honestly not sure if one has an advantage over the other, but you should definitely move off cross-undici-fetch if you can. I don’t think it’s broken, but it isn’t receiving updates anymore.
Haven’t looked further into it yet, but @razzeee I’m confirming that I’m also seeing a call to mockCurrentUser in one it block carry over into another on v4.
This has broken webauthn - I tested this with a brand new Redwood project (noting that the docs have not been updated.) and the issue persists. It looks like it’s sending back the page’s own index.html instead of the proper response, for some reason?