Comments (4)
this seems to work perfectly @felangel thank you! Just working on a small example, will share it across later.
from dart_frog.
Hi @dbilgin 👋
Thanks for opening an issue!
If I understood correctly, you should be able to define your own custom middleware and chain them via the use
API like:
import 'package:dart_frog/dart_frog.dart';
Handler middleware(Handler handler) {
return handler
.use(_customMiddleware())
.use(provider((context) => 'some text'));
}
Middleware _customMiddleware() {
return (handler) {
return (context) async {
final response = await handler(context);
print('custom middleware');
return response;
};
};
}
Let me know if that helps 👍
from dart_frog.
@felangel thanks a lot for your answer!
I seem to be stuck in the same place unfortunately. Sorry if it wasn't clear before, really appreciate the help.
So let's say I receive a JWT in the header and I want to decode this JWT and check its validity asynchronously and if it is invalid, I would return a response of 401, but if it is valid, I would get some data from it and pass it onto my route. What I want to do is basically something like below:
Handler middleware(Handler handler) {
return (RequestContext context) async {
final authHeader = context.request.headers['Authorization'].toString();
final String? jwtData = await resolveJWT(authHeader);
if (jwtData == null) {
return Response(statusCode: 401);
}
// ---> At this point I have checked the validity and returned 401 if necessary, after this
// check I would want to inject the
// `jwtData` into my route so that I can actually use the data for and I wouldn't have to decode the JWT
// separately everywhere. But here I can't call something like below I think:
handler.use(provider((context) => jwtData));
final response = await handler(context);
return response;
}
}
So in this case I would have to resolve the jwtData
one more time in my route separately or do you think there is a way to do this?
from dart_frog.
@dbilgin I put together an example of how this could look:
// routes/_middleware.dart
import 'dart:io';
import 'package:dart_frog/dart_frog.dart';
Handler middleware(Handler handler) {
return handler.use(headerValidator());
}
/// Checks for presence of a `foo` header.
/// If `foo` header is not present, returns a 403.
/// If `foo` header is present, provides the value via context.
Middleware headerValidator() {
return (handler) {
return (context) {
final request = context.request;
if (!request.headers.containsKey('foo')) {
return Response(statusCode: HttpStatus.forbidden);
}
final value = request.headers['foo'] as String;
return handler(context.provide<String>(() => value));
};
};
}
// routes/index.dart
import 'package:dart_frog/dart_frog.dart';
Response onRequest(RequestContext context) {
return Response(body: context.read<String>());
}
Let me know if that helps 👍
from dart_frog.
Related Issues (20)
- fix: No active package dart_frog_cli after activation HOT 2
- feat: Add support for middleware testing on `dart_frog_test` HOT 2
- fix: Using too much memory HOT 3
- docs: configure a `.vscode/launch.json` to execute "Dart Frog: Start and Debug Development Server" command HOT 1
- fix: ./pubspec_overrides.yaml generated with \ on windows breaks docker build. HOT 3
- fix: pre hook script: Null check operator used on a null value HOT 13
- fix: Flutter web CORS request handling HOT 2
- fix: VSCode With Incompatible `dart_frog` Version Causes Poor Performance HOT 5
- docs: include `hostname` daemon documentation
- How can we use frog as a android application also server run in background HOT 1
- fix: Test cases failing in dart_frog_cli while running dart test(OS errors)
- fix: need clarity around static files HOT 6
- chore: Dart Frog CLI 1.2.0 release
- max number of websockets HOT 3
- fix: Concurrency Issues HOT 2
- CI / CD Workflow Examples HOT 2
- Dart frog support Isolates ? HOT 2
- docs: Add Globe to Deployment section in Docs
- How to inject variables into context in a certain route HOT 4
- feat: allow $ in `new` commands
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dart_frog.