Polymer / pwa-helpers

Small helper methods or mixins to help you build web apps.
BSD 3-Clause "New" or "Revised" License
439 stars 48 forks source link

force routing #29

Closed mrherndon closed 6 years ago

mrherndon commented 6 years ago

I know this is only a simple router. But, I was trying to create a password-protected area of a site. I was attempting to run a quick check of the firebase user on navigation to the page and if there was no logged in user, force navigate them back to the homepage. I admit this is a little user-hostile, perhaps an error message on the page would be more appropriate. The other use case is the exact opposite and less hostile, If they are on the home page and sign in, I assume they are going to the user dashboard, so I want to force navigate there. I can't seem to work out a way to force nav without getting the router confused or the state mixed up in redux. Am I missing something, or is this just beyond the scope of this router?

web-padawan commented 6 years ago

As you mentioned, this is the really simple router and it is good as long as you want to use Redux actions and write all the logic yourself.

For your use cases, I suggest you to try vaadin-router which has a dynamic redirects feature, and a lot more.

jsilvermist commented 6 years ago

To force a redirect, you could simply push or replace a new state, then dispatch a popstate event:

window.history.pushState(null, document.title, '/some/place');
window.dispatchEvent(new PopStateEvent('popstate'));
mrherndon commented 6 years ago

That's pretty clever and much more consistent than what I ended up with. The great benefit with your method here is that it can be triggered the same from anywhere.

Thanks for the assist @jsilvermist!

notwaldorf commented 6 years ago

Yup, that's totally the way to do it. I'll send a PR to add a comment note about that.