🇺🇸
ModularBr Framework for Delphi
Portugues(PT)TelegramGitHub
  • Welcome to ModularBr
  • Getting to know
    • Introduction
      • Benefits
      • Requirements
      • Installing
  • Module
    • Exporting Binds
    • Importing Binds
  • Routes
    • Route Guard
    • Middlewares
  • Pipes
  • Dependency injection
  • Route Handlers
  • Usability
    • Horse and ModularBr
    • Desktop and ModularBr
  • CLI
  • Frequently Asked Questions
    • FAQs
      • How to contribute?
      • How to download and install?
      • What is InjectorBr?
      • What is ResultPairBr?
      • What is ECLBr?
  • Usage policy
    • License
Powered by GitBook
On this page
  1. Routes

Route Guard

Securing the routes of your project with ModularBr.

PreviousRoutesNextMiddlewares

Last updated 1 year ago

The Route Guard of ModularBr is a middleware that plays a crucial role in ensuring the security of application routes. Essentially, it enables you to create authentication or authorization logic so that only users with appropriate permissions can access specific routes.

For instance, imagine you have a route to access confidential user information such as banking or health data. With the Route Guard, you can implement a check to ensure that only authorized users can access that route, thus safeguarding such information.

To use the Route Guard in ModularBr, simply create a class that inherits from TRouteMiddleware and implement the Call method, which is a boolean class function responsible for verifying whether the user has the necessary permissions to access the route. This function can examine information such as the user type, access level, or any other relevant data for your application.

Next, add this class to the RouteModule() method of the route. This way, whenever someone tries to access that route, the class function will be executed to verify the user's permissions.

With the Route Guard, you can ensure that your routes are protected, and only authorized users will have access to sensitive information in your application.

Example of usage:

unit app.module;

interface

uses
  dmfbr.module,
  nfe.module;

type
  TGardNFeMiddleware = class(TRouteMiddleware)
  public
    class function Call(const AReq: IRouteRequest): boolean; override;
  end;
  
  TAppModule = class(TModule)
  public
    function Routes: TRoutes; override;
  end;

implementation

{ TAppModule }

function TAppModule.Routes: TRoutes;
begin
  // Rotas
  Result := [RouteModule('/nfe/:id', TNFeModule, [TGardNFeMiddleware])];
end;

{ TGardNFeMiddleware }

class function TGardNFeMiddleware.Call(const AReq: IRouteRequest): boolean;
begin
  Result := (AReq.Username = 'user') and (AReq.Password = '123456');
end;

end.