question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best practices for care some routes handler

See original GitHub issue

I 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:closed
  • Created 4 years ago
  • Comments:11 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
mefechoelcommented, Oct 22, 2020

Hmm, you could try to put the call to navigate in an onMount. Navigations that occure before the main Router’s mounting are swallowed (see related issue in svelte-navigator). The problem is, that you’ll loose the reactivity in onMount. So afterUpdate 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.

4reactions
sayhicoelhocommented, Sep 3, 2019

I’d like to see beforeEnter to run some piece of code whenever routing starts.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found