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 issue with multiple routes for the same page with different conditions #549

Closed
axos88 opened this issue May 15, 2019 · 7 comments
Closed

Comments

@axos88
Copy link

axos88 commented May 15, 2019

This currenty throws a RuntimeException(Unspecified action for page Dashboard at Path(/)) if the user is logged in:

    val loggedInRoutes = emptyRule |
      staticRoute("/", Page.Dashboard) ~> render(Dashboard())

    val routes = emptyRule |
      staticRoute("/login", Page.Login) ~> render(Login()) |
      (staticRoute("/", Page.Dashboard) ~> redirectToPage(Page.Login)(Redirect.Push))
        .addCondition(_ => CallbackTo { !Authentication.isLoggedIn })(_ => None) |
      loggedInRoutes.addCondition(_ => CallbackTo { Authentication.isLoggedIn })(_ =>
        redirectToPage(Page.Login)(Redirect.Push))

    routes.notFound(redirectToPage(Page.Dashboard)(Redirect.Replace)).renderWith(layout)

What I would like to achieve is the user to be redirected to the /login page if he tries to access a protected route without being logged in, and to be redirected to / if he tries to access a non-existent one. Since / is also a protected route currently, it should redirect to /login if the user is not authenticated.

Seems like the system gets confused by having to rules for the same Page with different conditions, which are mutually exclusive, so one of them should ALWAYS trigger. This shouldn't be a routing error.

@japgolly
Copy link
Owner

Just having a quick look here but it seems to me that if you remove the second definition of staticRoute("/", Page.Dashboard) it will work the way you expect. So:

    val loggedInRoutes = emptyRule |
      staticRoute("/", Page.Dashboard) ~> render(Dashboard())

    val routes = emptyRule |
      staticRoute("/login", Page.Login) ~> render(Login()) |
      loggedInRoutes.addCondition(_ => CallbackTo { Authentication.isLoggedIn })(_ =>
        redirectToPage(Page.Login)(Redirect.Push))

    routes.notFound(redirectToPage(Page.Dashboard)(Redirect.Replace)).renderWith(layout)

@axos88
Copy link
Author

axos88 commented May 16, 2019

Hmm, yeah, seems like it, I'll check this out.
But in general having this kind of setup is not supported? (I mean having the same route with multiple actions based on some mutually exclusive conditions that cover the whole probability space?).

@japgolly
Copy link
Owner

japgolly commented May 16, 2019 via email

@axos88
Copy link
Author

axos88 commented May 17, 2019

By failing early what do you mean? compile time, or runtime? The runtime exception that occurs, doesn't really satisfy the early failing though, so I guess this is either a bug that needs fixed / feature that has to be added, or a better error message added.

@japgolly
Copy link
Owner

japgolly commented Aug 4, 2019

Hey, FYI, for personal reasons I'm not finding myself with much time to spend on OSS lately. I hope that will get a bit better soon but I do get time I've already got a lot of other stuff to do (esp. Scala 2.13 upgrades) so I don't see myself being able to prioritise this issue for a long while. I do try to give PRs higher priority (esp when they're complete) so that's probably your best bet at helping get this resolved anytime soon.

@axos88
Copy link
Author

axos88 commented Aug 4, 2019

Gotcha!

@japgolly
Copy link
Owner

japgolly commented Jan 6, 2020

Closed via #616

@japgolly japgolly closed this as completed Jan 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants