Code Monkey home page Code Monkey logo

Comments (4)

dbilgin avatar dbilgin commented on May 27, 2024 2

this seems to work perfectly @felangel thank you! Just working on a small example, will share it across later.

from dart_frog.

felangel avatar felangel commented on May 27, 2024

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.

dbilgin avatar dbilgin commented on May 27, 2024

@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.

felangel avatar felangel commented on May 27, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.