Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

router guard and async #125

Closed
Manborough opened this issue Jul 19, 2020 · 9 comments · Fixed by #135
Closed

router guard and async #125

Manborough opened this issue Jul 19, 2020 · 9 comments · Fixed by #135
Labels
enhancement New feature or request ready Change implemented in a dev branch
Milestone

Comments

@Manborough
Copy link

Manborough commented Jul 19, 2020

Is there a particular way I need to use async functions within the wrap function?
I'm trying to check authorisation from my API roughly like this.

    '/login': Login,
    '/facility': wrap(
      Facility,
      {},
      async (detail) => {
        const result = await authQuery()
        return await result
      }
      ),
    '*': Login,
  }

but I'm finding the route loads before the results from the api return.

@dkchiem
Copy link

dkchiem commented Jul 19, 2020

I also have this issue. I need to verify that the user is connected with the firebase API.

@BogdanDarius
Copy link

BogdanDarius commented Jul 24, 2020

Hi!
You could specify in routes config a wrapper component:

let routes;
routes = new Map();
routes.set("/protected-route", MyWrapper);

And in MyWrapper you could do something like this:

{#await isAuthenticated()}
// Show progress
{:then value}
// Load component with Loadable or svelte:component
  <Loadable loader={AccessGrantedComponent} />
{:catch error}
 // Show error
{/await}

@Dan1ve
Copy link

Dan1ve commented Aug 1, 2020

I have the same issue, and I feel like the suggestion by @BogdanDarius isn't general-purpose enough to (easily) work on many pages (please correct me if I misunderstood something).
Simply passing an async function to wrap would be really nice!

@BogdanDarius
Copy link

I have the same issue, and I feel like the suggestion by @BogdanDarius isn't general-purpose enough to (easily) work on many pages (please correct me if I misunderstood something).
Simply passing an async function to wrap would be really nice!

Promise and lazy routing are on the roadmap.
I encountered same issue while I wanted to implement code splitting.

@Dan1ve
Copy link

Dan1ve commented Aug 1, 2020

That's great to hear @BogdanDarius , really looking forward to this!

@keenonsomething
Copy link

Encountering this issue with firebase authentication as well, would love to see this feature implemented.

@Dan1ve
Copy link

Dan1ve commented Aug 16, 2020

FWIW, I now call a redirectIfNotAuthenticated() method at the top of every <script> tag of protected routes. This method checks the authentication against the server and, if necessary, redirects to the login page (setting the current page as url argument for redirect back after login). Although not an ideal solution, this seems to work fine as a workaround.

@ItalyPaleAle
Copy link
Owner

I do the same as @Dan1ve in my own code.

If every single route requires authentication, I check it in a function that's called at the beginning of the page's execution, in the main.js file (outside of the Svelte app)

@ItalyPaleAle ItalyPaleAle added this to the v3.0 milestone Sep 20, 2020
@ItalyPaleAle ItalyPaleAle added enhancement New feature or request ready Change implemented in a dev branch labels Sep 20, 2020
@ItalyPaleAle
Copy link
Owner

@Manborough support for async functions in route pre-conditions has been added to 3.0 (WIP branch) and will be released with version 3.0 soon 🚀

ItalyPaleAle added a commit that referenced this issue Sep 20, 2020
This also includes a refactoring to leverage async/await more
Fixes #125
@ItalyPaleAle ItalyPaleAle mentioned this issue Sep 20, 2020
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request ready Change implemented in a dev branch
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants