Dart Frog does not support having multiple routes in a single file, as it is a file-based routing framework.
What you can do however is setup the routes as you have in the screenshot, and define both a middleware and inject an AuthManager
:
// In your _middleware.dart
final _authManager = AuthManager();
Handler middleware(Handler handler) {
return handler
.use(provider<AuthManager>((_) => _authManager));
}
// In any of your routes
FutureOr<Response> onRequest(RequestContext context, String id) async {
final authManager = context.read<AuthManager>();
// Do something with authManager
}
If you want your AuthManager
to be per request, instead of using a global value you can construct it every time the create
, that is the method you pass to the provider, is called:
// In your _middleware.dart
Handler middleware(Handler handler) {
return handler.use(
provider<AuthManager>((context) {
// Do something with context
return AuthManager(...);
}),
);
}
See the docs about dependency injection for more information