Best practices for care some routes handler
See original GitHub issueI know that is possible to implement the authentication logic at the beginning of each route handler. In the following code the protected routes should only be visible after the user authentication. My concern is about the best pratices to care some routes handler.
Is this the best approach to implement a separation between public and protected routes handler?
<script>
import { Router, Route, navigate } from "svelte-routing";
import NavLink from "./components/NavLink.svelte";
import Home from "./routes/Home.svelte";
import About from "./routes/About.svelte";
import Login from "./routes/Login.svelte";
import { currentUser } from "./stores/current_user.js";
// Used for SSR. A falsy value is ignored by the Router.
export let url = "";
let showNav = false;
$: if ($currentUser.token) {
showNav = true
navigate("/", { replace: true });
} else {
showNav = false
navigate("/login", { replace: true });
}
</script>
<Router url="{url}">
{#if showNav}
<nav>
<NavLink to="/">Home</NavLink>
<NavLink to="about">About</NavLink>
</nav>
{/if}
<div>
<Route path="/" component="{Home}" />
<Route path="/about" component="{About}" />
<Route path="/login" component="{Login}" />
</div>
</Router>
Issue Analytics
- State:
- Created 4 years ago
- Comments:11 (1 by maintainers)
Top Results From Across the Web
Route Optimizing Software: 6 Best Practices for Implementation
Route Optimizing Software Best Practices · 1. Begin with the End in Mind · 2. Obtain Internal Support for the Route Optimizing Software...
Read more >Chapter 4. Routes and handlers in-depth - hapi.js in Action
Routes are how you tell hapi what kind of requests you're interested in. Handlers are how you choose to respond to those routes....
Read more >Actions, Routes, Handlers & Methods | by Tom Chizek - Medium
Net Core Actions, Routes, Handlers & Methods, the article briefly discusses the four ... Methods are the best defined and best understood.
Read more >Routing and Handlers :: Yesod Web Framework Book
A single handler function for all request methods on a given route. · A separate handler function for each request method on a...
Read more >Which Go router should I use? (with flowchart) - Alex Edwards
By supports method-based routing I mean that the router makes it easy to dispatch a HTTP request to different handlers based on the...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hmm, you could try to put the call to
navigate
in anonMount
. Navigations that occure before the mainRouter
’s mounting are swallowed (see related issue in svelte-navigator). The problem is, that you’ll loose the reactivity in onMount. SoafterUpdate
might be the better place to put it. But because your component is always rendered, it will redirect on every route, when the user isn’t logged in. That’s why I put the guard in a second component inside the Route. That way, it is only rendered whe the route matches and only then redirects.I’d like to see
beforeEnter
to run some piece of code whenever routing starts.