schultek / jaspr

Modern web framework for building websites in Dart. Supports SPAs and SSR.
https://jasprpad.schultek.de
MIT License
1k stars 59 forks source link

fix: jaspr_router top level redirect #190

Closed walsha2 closed 3 months ago

walsha2 commented 3 months ago

Description

https://pub.dev/documentation/go_router/latest/topics/Redirection-topic.html

Per the go router documentation above, the following is an example and an excerpt:

redirect: (BuildContext context, GoRouterState state) {
  if (AuthState.of(context).isSignedIn) {
    return '/signin';
  } else {
    return null;
  }   
},

There are two types of redirection:

Example

Take the following example. The routerProvider is listening for authStateProvider changes. All of this triggers correctly - when the auth state changes the router is rebuilt. However, upon entering the Router constructor, the redirect method is NOT called the first time around. I would expect that since this is a top level redirect, this would happen before any navigation to see if the router needs to redirect due to the new state.

The same code/logic in a flutter app using go_router would first try to resolve the redirect on any rebuild (in this case to check for relevant state changes).

Maybe this was missed in the implementation port?

final routerProvider = Provider<Router>(
  (ref) {
    final authState = ref.watch(authStateProvider);
    return Router(
      routes: [
        Route(
          path: '/login',
          builder: (context, state) => Login(),
        ),
        Route(
          path: '/home',
          builder: (context, state) => Home(),
        ),
      ],
      redirect: (context, state) {
        if (authState.user == null) {
          return '/login';
        }
        return null;
      },
    );
  },
);

TLDR, the router is not resolving the redirects first when it is constructing the router. In turn, any actions that need to be taken are not getting triggered. This is making any sort of auth very messy and awkward to implement.

Configuration

Actually overriding all of these and using the latest main branch as of a6fbb30d95362723304cded037eb6f22212110bf

[✓] Jaspr CLI (Version 0.10.0)
  • Dart Version 3.3.1 (stable) (Wed Mar 6 13:09:19 2024 +0000) on "macos_arm64" at /Users/wma/opt/flutter/bin/cache/dart-sdk/bin/dart
  • Running on macos Version 14.4 (Build 23E214) - Locale en-US
  • Analytics: Enabled

[✓] Current Project
  • Dependencies on core packages:
    • jaspr: ^0.10.0
    • jaspr_builder: ^0.10.0 (dev)
    • jaspr_web_compilers: ^4.0.9 (dev)
    • jaspr_riverpod: ^0.3.9
    • jaspr_router: ^0.3.1
  • Uses server-side rendering: true
  • Uses experimental compilers: true
  • Uses flutter embedding: false

Related Issue

None of the redirects are actually changing the URL path in the address bar. I would expect that they would?

schultek commented 3 months ago

@walsha2 can you test if the linked pr correctly resolves the issue for you? (You can set the branch using ref for an overridden git dependency)

walsha2 commented 3 months ago

@schultek just tested - works exactly as expected on the branch. Nice job. Redirects are getting triggered anytime the router is rebuilt.