vaadin / platform

Vaadin platform 10+ is a Java web development platform based on Vaadin web components. If you don't know to which repository your bug report should be filed, use this and we'll move it to the right one.
https://vaadin.com
515 stars 76 forks source link

React hybrid routing #4710

Open mshabarov opened 6 months ago

mshabarov commented 6 months ago

Description

Support routing to Flow views in React+Hilla applications using React Router.

Use cases

As a developer I want to extend my React+Hilla application with server-side Flow views
So that I can benefit from Flow provided features, e.g. server-side navigation, Push and others.

As a developer I want to extend my Flow application with React-based views/components using Hilla
So that I can benefit from using React where I want.

Acceptance criteria

General criteria

  1. Flow applications that are not hybrids keep using Vaadin Router just like today.
  2. No need for a new routing API for Hilla, this isn't in scope of this project.
  3. This acceptance criteria covers the routing aspect of integration between Flow and React. Other aspects such as reusing 3rd party React components or using React as a template language from Flow are outside the scope of this project.
  4. React router integration is turned on by feature flag and Flow gives a clarification message to developer about the need of adding this feature flag.
  5. Flow tracks usage of hybrid configuration, including React+Hilla+Flow, Lit+Hilla+Flow cases.
  6. start.vaadin.com includes starter project for React+Hilla+Flow, having Hilla and Flow views and feature flag turned ON by default.

Security

mshabarov commented 6 months ago

navigation events and server-side navigation

Tested Hilla view -> Flow view navigation and navigation lifecycle events: BeforeLeaveEvent, BeforeEnterEvent, AfterNavigationEvent, including .addBeforeEnterListener() listener and forwardTo/rerouteTo methods. Works as expected.

caalador commented 5 months ago

Minimal documentation on getting the combination to work

Adding Flow to a Hilla-react project. (sample from CRM tutorial)

    <properties>
        <java.version>17</java.version>
        <vaadin.version>24.3-SNAPSHOT</vaadin.version>
        <hilla.version>2.4.0</hilla.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.vaadin</groupId>
                <artifactId>vaadin-bom</artifactId>
                <version>${vaadin.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- Other pom imports like hilla -->
        </dependencies>
    </dependencyManagement>
caalador commented 5 months ago

Enabling eagerServerLoad in a Flow project works as expected. Also couldn't see any issues in a hilla-flow-react hybrid after enabling the flag.

mshabarov commented 5 months ago

Access control seems to work fine for Flow view.

1) Navigating from Hilla view to protected Flow view reroutes to login view if not authenticated. 2) Navigating from Hilla view to unauthorised Flow view shows "Could not navigate to..." standard page.

However, one thing that doesn't work is navigating to Hilla view that is protected by a role. For some reason, this view redirects back to login view, even after successful login. Public Hilla view doesn't have this problem.

In the browser console log I see that UserInfoService returns "ROLE_USER" to the client and login method returns no errors.

Maybe my project configuration is incorrect. I followed this tutorial https://hilla.dev/docs/react/guides/security/spring-login.

Here is my test project. proto-hilla-react-hybrid.zip

@platosha could you please verify my test project when appropriate ?

caalador commented 5 months ago

com.vaadin.experimental.reactRouter=true

 import {serverSideRoutes} from "Frontend/generated/flow/Flow";

 ...
 export const routes = [
    // Hilla definitions
    ...serverSideRoutes
 ]
platosha commented 5 months ago

So the issue with React routes redirect was that the server returns {"authorities": ["ROLE_USER"]}, whereas the Hilla helper expects {"roles": ["USER"]}. This implementation mismatch is not related with hybrid routing and is easy to workaround by specifying client-side auth configuration with getRoles.