allows for a cleaner way of handling authenticated routes#332
Conversation
|
Just been thinking about this. Could also have a more generic use for this. const Routes = () => {
const { user } = useAuth()
const handleRouteChange = ({ navigate, private: isPrivateRoute, ...props }) => {
if (!user && isPrivateRoute) navigate('/login')
}
return (
<Router onRouteChange={handleRouteChange}>
<SettingsPage path="/settings" private />
<LoginPage path="/login" />
</Router>
);
} |
|
I think this should be called a hook, and I think there should be an object passed to the hook that contains the URL the user is navigating to, and maybe specify the full URL as well as the relative URL. |
|
@alex-cory great, that's exactly how i'd like to have it! The right spot to handle such stuff. |
|
I don't foresee this router taking an active stance on how to handle authenticated vs non-authenticated routing. This is best solved in user-land as different cases require different setups. Thanks for the contribution though! |
|
@blainekasten that's why I suggest the |
|
+1 @blainekasten what is the problem with the |
|
Supporting an Like mentioned, this could be handled in user land with a composable hook that redirects. |
|
Hm... I'm curious how it would cause a flicker. Is it because |
| if (onPrivateRoute && element.props.private) { | ||
| onPrivateRoute(props.navigate); | ||
| } | ||
|
|
There was a problem hiding this comment.
I think you need to treat the function as if it would return a Boolean describing the bailout:
// peasant javascript
if(typeof onBeforeRender === 'function' && !onBeforeRender(props) ){
return ''
}Then the remaining render code below won't run, and won't cause flicker, or more importantly avoid fail states in the rendered component due to possible missing values that can be checked for in the above onBeforeRender
|
At the moment we deal with this by wrapping the components we want to associate to a route with either of two components: They both take props:
They both check :
If the checks fail for the private route, it will :
After loginSuccess the user is sent back to where they tried to go. So any solution that tries to allow logic when the matched route is about to render, but before actual rendering, needs to also determine if rendering should continue based on a return value. |
|
vue-router has |
Why r u closing this issue, pls go to try vue router beforeEach, u will know why so many people moving to vue js & vue router as well as people request it. |
|
@liho98 VueJS is an overhyped framework which sucks in my opinion. And you don't need a and so |
seems like u dont know how vue router beforeEach exactly works. Based on your example, if i have 100 routes, i need to wrapped the function 100 times, this is sucks! |
|
@liho98 Seems like you are just not experienced enough to find solutions on your own. :) |
lol, u failed to get what i mean exactly, based on this array example, same its wrapped the suck function 100 times. What i want is without wrapping the route, listen to an beforeEnter event or a beforeEnter callback just like how vue router works, handling it elegantly. |
Allows for syntax like this