-
Notifications
You must be signed in to change notification settings - Fork 397
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
Add an option to disable fetching in UserProvider #313
Comments
Hi @adarnon - performing the fetch to
Have you considered writing your own context and using that instead? // use-user.js
export const UserContext = React.createContext(
user: undefined;
);
// _app.js
const App = ({ Component, pageProps }) => (
<UserContext.Provider value={{ user: pageProps.user }}>
<Component {...pageProps} />
</UserContext.Provider>
); |
@adamjmcgrath I've considered using my own provider (that's what I've been doing before v1.0 was released), but I was hoping to migrate this the built-in one so I that I can also use the other provided utilities |
Ah yep - that makes sense.
Can you give me a more concrete example (do you have some code or example app)? I'm still not sure why you'd want to use the
This sounds reasonable, can you tell me more about what specifically you'd want to export, and how that would fix your problem? |
I can't speak for @adarnon, but at least in our implementation, we have the However, what this results in is a 401 console error when it's trying to call
|
I have a similar case like @austinhale. Another example - we use Segment for analytics. I wrapped our |
If |
Thanks for the context @adarnon - I'll raise some work to export |
In my use case, I have an app that uses a different layout in the same route based on the authenticated state of the user. I'll call them "app layout" and "marketing layout". If the user is not logged in, However, I want my users to be able to access that original marketing layout once they are logged in, so I expose it at Within the navigation bar in marketing layout, I want to display either a login/signup UI or an account menu UI based on the authenticated state. To do this, I need to call If I use the custom A fairly specific use case I realize, and those error messages are not breaking, just annoying. Could there be an option for the |
Describe the problem you'd like to have solved
Hi,
In my app, I have a few pages where I don't want the user object to be loaded at all. These are public-facing pages and I want to save the unnecessary fetches to
/api/auth/me
because the user object shouldn't be available there.Due to the implementation of
UserProvider
anduseUser
, aUserProvider
MUST exist in the App tree in order to calluseUser
, otherwise an exception is thrown. Some of my components call theuseUser
hook internally, and I'd like them to returnuser
as undefined in that case. However,UserProvider
will ALWAYS fetch/api/auth/me
, so there is no way to avoid that.Describe the ideal solution
I think that a prop flag could be used in order to instruct
UserProvider
not to perform any fetching. In that case, it would either use the user object provided in theuser
prop (e.g., from SSR) or nothing.Another solution would be to export the
ReactContext
object (User
), so that the context provider may be manually overridden. This could also be useful for unit testing, e.g. to mockcheckSession()
.Alternatives and current work-arounds
A current workaround is to pass
profileUrl=""
toUserProvider
in these routes, causing the fetch to fail.Additional information, if any
The text was updated successfully, but these errors were encountered: