Middleware
Middleware serves as a tool to intercept navigation between screens under specific conditions. For instance, when a user attempts to navigate to a screen that requires authentication, but the user is not signed in, the Middleware intercepts the navigation and redirects the user to the sign-in screen.
Middleware is created in two steps:
Create a Middleware Annotation
First, create a middleware annotation and mark it with the
@Middleware
annotation:
Create a Middleware Controller
Next, create a Middleware controller by inheriting from
MiddlewareController
orMiddlewareControllerComponent
. Mark it with the previously created annotation to connect this controller to the annotation:
From this point, you can use your annotation to mark a RouteController
, indicating that it should call this middleware before navigating to the controller's screen.
The difference between MiddlewareController
and MiddlewareControllerComponent
is that the latter includes the onInject
method. This method is called after the middleware is created and provides the instance of the component for injection.
Full code example:
Last updated