mefechoel / svelte-navigator

Simple, accessible routing for Svelte
Other
502 stars 39 forks source link

route guard #36

Closed sysmat closed 3 years ago

sysmat commented 3 years ago

Is your feature request related to a problem? Please describe.

To have guarded some routes(admin, user, ...) something like angular router, so function which determines if the route can be activeted

Describe the solution you'd like

<script lang="ts">
function canActivate():boolean {
}
</script>
<Router {basepath}>
  <main>
    <Navbar />

    <Route path="queries" component={Queries}  />
    <Route path="statistics" component={Statistics} canActivate={canActivate}/>
    <Route path="status" component={Status} />
  </main>
</Router>

Describe alternatives you've considered

<Router {basepath}>
  <main>
    <Navbar />

    <Route path="queries" component={Queries}  />
     {#if canActivate}
        <Route path="statistics" component={Statistics} />
     {/if}   
    <Route path="status" component={Status} />
  </main>
</Router>
mefechoel commented 3 years ago

Something very similar to what you're describing is already possible with a couple of helper components. Checkout the private routes example as a starting point. You could pass a check function and a redirect target (if you should need that) to the protected route as props.

I hope this works for your use case.