Giter Club home page Giter Club logo

Comments (6)

stubailo avatar stubailo commented on May 12, 2024 1

The question here is, if someone built an alternate network interface, would an existing login middleware work with it? It seems like the answer is probably no, because only an HTTP network interface would have a login middleware that uses headers. So it makes sense that it would be attached to the network interface I guess.

Everything you said sounds good then.

from apollo-client.

jbaxleyiii avatar jbaxleyiii commented on May 12, 2024 1

@stubailo that is what @johnthepink had too 👍 Let's go with that direction for the auth middleware

from apollo-client.

johnthepink avatar johnthepink commented on May 12, 2024 1

Closed via #75

from apollo-client.

jbaxleyiii avatar jbaxleyiii commented on May 12, 2024

A few notes:

  • the middleware can modify both the options passed to the request, and the headers by passing a compliant RequestInit object
  • we should document that the following fields are reserved body, headers.Accept, headers.Content-Type, method and probably warn if they are sent
  • the authentication middleware should only affect the headers. (Unless I'm missing another need?)

Since the network interface can be supplied to the client, I think having the middleware be attached to it makes the most sense. We could pretty easily return a way to apply middleware to the default network interface through the client object. public registerNetworkMiddleware seems a good enough solution

from apollo-client.

jbaxleyiii avatar jbaxleyiii commented on May 12, 2024

@stubailo @johnthepink and I talked through a few options for this. What are your thoughts on https://gist.github.com/jbaxleyiii/e899fe74842b7488657617e43c9a30fe

// the meat of the gist

const heighliner = createNetworkInterface('http://localhost:8888/');

interface MiddlewareRequest {
  request: Request;
  options: RequestInit;
}

// option 1, generic middleware that can modify the options of the request
const tokenMiddleware = (request: MiddlewareRequest, next: function): void => {
  // this would be called on each request and has the option to modify the headers
  // this doesn't return a value, but rather modifies the request inline.
  // that feels gross but I don't know of another way to do next() unless you pass
  // the request into next? which also seems odd
};

// option 2, authorization sugaring of option 1 that only modifies the headers
// and defaults to setting an 'Authorization' header.
heighliner.authorize('token': string, /* optional key name for token : string*/);
heighliner.removeAuthorization(/* probably just a null value here? */);


// the middleware would be on the networkInterface itself
// should this be an array? or allow for multiple arugments to be passed as args
// e.g. `use(middleware1, middleware2, etc)
heighliner.use(tokenMiddleware);

from apollo-client.

stubailo avatar stubailo commented on May 12, 2024

heighliner.authorize

is this better than something like:

const authMiddleware = new AuthTokenHeaderMiddleware(); 

heighliner.use([
  authMiddleware,
]);

authMiddleware.setToken(token);
authMiddleware.setToken(null);

Also, I think use should always take an array probably.

Other than that, looks really good! I think going with the express model where you modify the object is probably the best.

from apollo-client.

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.