Coder Social home page Coder Social logo

Lifecycle hook? about routify HOT 7 CLOSED

roxiness avatar roxiness commented on September 1, 2024
Lifecycle hook?

from routify.

Comments (7)

khuongduybui avatar khuongduybui commented on September 1, 2024 1

Got it. I will definitely give it a try this weekend!

from routify.

jakobrosenberg avatar jakobrosenberg commented on September 1, 2024

Not currently. I resolve auth in App.svelte.

{#if authorized}
<Router />
{/if}

Do you have a specific use case in mind?

from routify.

khuongduybui avatar khuongduybui commented on September 1, 2024

How do I render my login / signup and other public pages then?

from routify.

jakobrosenberg avatar jakobrosenberg commented on September 1, 2024

There are a few approaches depending on the nature of your app.

If it's a strict admin dashboard

  • redirect to pages/login.svelte and change the condition to
    {#if authorized || ['login', 'register'].includes(location.pathname)}
  • you could also just render a login page whenever a user isn't logged in, regardless of the pathname.
{#if authorized}
  <Router />
{:else}
  <Login>
{/if}

If only parts of your app is restricted to authorized users, like an admin module, you could include the conditional in the _layout.svelte / _reset.svelte of the module. Then any page within the module is subject to the condition.

from routify.

khuongduybui avatar khuongduybui commented on September 1, 2024

Thanks for the explanation.
I've been using _layout.svelte for authn / authz logic in Sapper preload() hook, but since we are not using Sapper, I'm not sure where to put the logic in.

Basically what I want is just some logical redirection:

  • If visitor has not logged in, member-only pages will redirect to login page
  • If visitor has logged in but is not admin, admin-only pages will redirect to home page
  • Unknown routes redirect to fallback page (home or a custom 404)

from routify.

jakobrosenberg avatar jakobrosenberg commented on September 1, 2024

_layout.svelte is a confusing name at times. I would have preferred _modules.svelte, but that could be confusing to people coming from Sapper.

The most straight forward approach would be a separate module for members and admins. Ie. src/pages/admin/_layout.svelte and src/pages/members/_layout.svelte

If you'd prefer more granularity, you could (should) authorize in App.svelte and include the user and/or user-role in the scope.

Once something is added to the scope, it's available to all components nested through router.

<!-- App.svelte-->
<script>
  let user = false
  authorizingScript().then((payload)=>{
    user = payload.user
})
</script>

{#if user}
  <Router scoped={{user}}>
{:else}
  <!-- Loading animation or text -->
{/if}
<!-- src/pages/notifications/_layout.svelte-->
<script>
  export let user
  $: user.isGuest && history.pushState(null, null, '/login');
</script>

{#if !user.isGuest}
<-- content -->
{/if}

from routify.

jakobrosenberg avatar jakobrosenberg commented on September 1, 2024

For 404s, there's _fallback.svelte. You can have multiple fallbacks. Ie. src/pages/_fallback.svelte and src/pages/admin/_fallback.svelte. The first fallback found while traversing back through the tree is used.

from routify.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.