Skip to content

[go_router] Prevent access to route when navigated to via a deep link #103659

Closed
flutter/packages
#5742

Description

@DamienMrtl

Use case

Prevent access to a route with deeplink.
In the redirect function I would like to detect if the navigation event is a deeplink and redirect to the parent route.

Proposal

Add a bool in the state that says if its deeplink or not

Activity

  1. added
    in triagePresently being triaged by the triage team
    packageflutter/packages repository. See also p: labels.
    c: proposalA detailed proposal for a change to Flutter
    c: new featureNothing broken; request for a new capability
    and removed
    in triagePresently being triaged by the triage team
    on May 13, 2022
  2. added
    P3Issues that are less important to the Flutter project
    on May 17, 2022
  3. chunhtai commented on May 19, 2022

    @chunhtai
    Contributor

    Can you explain more on your use case?

  4. vimhead commented on May 20, 2022

    @vimhead

    By default, any deep link is mapped to route configuration, even if route is not supposed to be accessed through deeplink.
    I suppose OP wants to disable deep linking to some pages. @chunhtai

  5. DamienMrtl commented on May 23, 2022

    @DamienMrtl
    Author

    @chunhtai Yes I'm using Flutter Web and I want some routes to be accessed only by the user navigating "in-app" and not by entering the URL directly. For now I'm manually setting the extra argument to true when navigating in app, so I can differentiate between deeplinks or user navigation.

  6. chunhtai commented on May 23, 2022

    @chunhtai
    Contributor

    Thanks for explanation, I think instead of add a boolean to the state, we should have a more comprehensive API to handle Deeplink

    For example

    GoRouter(
      ....
      onDeepLink: (RouteInformation info) => ....
    )

    and by default , it will go through the regular parsing if this method is not provided

  7. DamienMrtl commented on May 23, 2022

    @DamienMrtl
    Author

    @chunhtai It's a good idea yes, but maybe the function onDeepLink should also be supported on each GoRoute(...). So we don't have to do some complicated "if else" or "switch case" for each routes we want to intercept in the global onDeepLink function.

  8. horo99 commented on Jun 13, 2022

    @horo99

    @chunhtai I need this feature also.
    But it maybe more suitable with a wider concept rather than deeplink merely.
    The wider concept is preventing an access going forward.
    By the way, is there any other method to prevent an access?

    Thanks for explanation, I think instead of add a boolean to the state, we should have a more comprehensive API to handle Deeplink

    For example

    GoRouter(
      ....
      onDeepLink: (RouteInformation info) => ....
    )

    and by default , it will go through the regular parsing if this method is not provided

  9. 13 remaining items

  10. chunhtai commented on Jul 21, 2023

    @chunhtai
    Contributor

    I think there are two steps to fix this issue

    1. https://github.com/flutter/packages/pull/4392/files
      This adds the ability to know whether a GoRouteState is a deeplink (by checking GoRouteState.uri.host and GoRouteState.uri.scheme)
    2. [go_router] Replace redirect with onEnter and onExit #102408
      The onenter and onexit should have more flexible api to decide whether to push page or doing other operation.
  11. Harris-Thomas commented on Oct 28, 2023

    @Harris-Thomas

    Hi, when is this scheduled to release ? I have the same requirement to restrict deeplink, without going through login page.

  12. ChopinDavid commented on Dec 15, 2023

    @ChopinDavid

    I think there are two steps to fix this issue

    1. https://github.com/flutter/packages/pull/4392/files
      This adds the ability to know whether a GoRouteState is a deeplink (by checking GoRouteState.uri.host and GoRouteState.uri.scheme)
    2. [go_router] Replace redirect with onEnter and onExit #102408
      The onenter and onexit should have more flexible api to decide whether to push page or doing other operation.

    You mention that you can tell whether a GoRouteState is a deep link by checking GoRouteState.uri.host and GoRouteState.uri.scheme. Both of these values are "" when I check them in the redirect, does this sound right?

    If so, the only way to tell if a state is a deep link or initiated from within the app is to add some extra denoting that it is not a deeplink, as others have suggested?

  13. ChopinDavid commented on Dec 17, 2023

    @ChopinDavid

    @chunhtai I was able to get back a GoRouteState.uri.host and GoRouteState.uri.scheme in our redirect with, seemingly, no side-effects by making the following changes:

    I can get moving on making a PR for this if this makes sense to you...

  14. chunhtai commented on Dec 18, 2023

    @chunhtai
    Contributor

    You mention that you can tell whether a GoRouteState is a deep link by checking GoRouteState.uri.host and GoRouteState.uri.scheme. Both of these values are "" when I check them in the redirect, does this sound right?

    This is expected as of now, since go_router can't migrate to use RouteInformation.uri yet.

    Edit: looks like the stable has reach 3.12.0 and we should be able to use it now

  15. chunhtai commented on Dec 18, 2023

    @chunhtai
    Contributor

    I can get moving on making a PR for this if this makes sense to you...

    yes, feel free to send out a pr.

  16. chunhtai commented on Dec 18, 2023

    @chunhtai
    Contributor

    Actually I was wrong package is supporting 3.10.0 and up, so the routeinformation.uri isn't yet available.

    update: just consulted with eco team, it looks like we can bump the version to latest stable if we want to.

  17. ChopinDavid commented on Dec 19, 2023

    @ChopinDavid

    @chuntai So, we would need a PR that:

    1. Upgrades go_router's min. flutter version to >=3.12.0 (so that RouteInformation.uri exists)
    2. Implements my aforementioned changes which make use of RouteInformation.uri rather than RouteInformation.location
    3. Adds new tests/makes sure that no tests are broken

    correct?

  18. chunhtai commented on Dec 20, 2023

    @chunhtai
    Contributor

    @ChopinDavid that sounds correct

  19. ChopinDavid commented on Dec 22, 2023

    @ChopinDavid
  20. github-actions commented on Feb 23, 2024

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  21. locked as resolved and limited conversation to collaborators on Feb 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3Issues that are less important to the Flutter projectc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterp: go_routerThe go_router packagepackageflutter/packages repository. See also p: labels.

Type

No type

Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions