-
-
Notifications
You must be signed in to change notification settings - Fork 797
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
[legacy-framework] Add support for Page.redirectAuthenticatedTo
to be a function with access to session
#2634
Conversation
Awesome, thank you!
We can't do this, because it will cause React hydration errors. This is the reason useSession is empty on first render. This is a fundamental law of React that we have to live with. The solution is use So remove publicData, and then the last thing is opening a PR to the docs repo to update the docs for this :) |
@flybayer here is one more addition. redirectAuthenticatedTo now gets called only when session is not isLoading. i think this is a better option instead of letting users to do some extra checks in their code: if(!session.isLoading) {
return session.role === 'admin' ? '/admin' : '/';
} |
@s-r-x ah.. I was mistaken — I'm sorry! In this case, since we are redirecting, we don't have to wait for second render. So we should do like you first had it, except pass redirectAuthenticateTo({session: publicDataStore.getData()}) And the TS type should be Make sense? |
@flybayer yep |
…tedTo function / add false as a possible redirectAuthenticatedTo value
@flybayer done. Page.redirectAuthenticatedTo = ({ session }) => {
return session.role === 'admin' ? false : '/401'
} |
packages/core/src/blitz-app-root.tsx
Outdated
} | ||
if (redirectAuthenticatedTo && typeof redirectAuthenticatedTo !== "string") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be an else if
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dont't think so. the second if block would never fire(in case if it's "else if") if redirectAuthenticatedTo is a function. but one of the possible return values from this function is an object, which one we should transform to a string inside the second if block on line 63. correct me if i'm wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@s-r-x I think @flybayer is correct because redirectAuthenticatedTo
will be truthy since it's a function and typeof redirectAuthenticatedTo !== "string"
will also be true
because "function" !== "string"
.
Probably adding a test case would surface this?
if (typeof redirectAuthenticatedTo === "function") {
redirectAuthenticatedTo = redirectAuthenticatedTo({session: publicData})
}
// `redirectAuthenticatedTo` will be truthy since it's a function
// and "function" !== "string" will equal true, so the second `if` block will run.
if (redirectAuthenticatedTo && typeof redirectAuthenticatedTo !== "string") {
redirectAuthenticatedTo = formatWithValidation(redirectAuthenticatedTo)
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically the code will work fine as is, but it's better to be an else if
from a code readability standpoint. Easier to instantly see that only one of these branches will run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Peek.2021-08-12.08-49.mp4
this is still fine because RedirectError accepts not only a string, but also RouteUrlObject(as far as i understand. at least in video it looks like it does) .
but the second if block is never fire in this case, and tbh i'm not quite sure how important that formatWithValidation call is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@flybayer i simplified the code, made everything immutable
…tAuthenticatedTo function has been called
Awesome, thank you!! |
Page.redirectAuthenticatedTo
to be a function with access to session
Added @s-r-x contributions for code |
Page.redirectAuthenticatedTo
to be a function with access to session
Page.redirectAuthenticatedTo
to be a function with access to session
Hello ^-^
This PR closes blitz-js/legacy-framework#64
redirectAuthenticatedTo can now be also a function. It accepts a context, which includes
i added a publicData because in my use case i need access to some user metadata on the first render, when session is empty.
What are the changes and their implications?
Bug Checklist
Feature Checklist