Skip to content

Inject app object to middleware factories #81

Closed

Description

@bajtos

Per @raymondfeng comment in #75:

I would also need to access the app instance either via the parameter injection or set by boot to the factory/handler instance.

Activity

  1. self-assigned this
    on Dec 10, 2014
  2. added this to the #Epic: Components milestone on Dec 10, 2014
  3. bajtos commented on Dec 10, 2014

    @bajtos
    MemberAuthor

    @raymondfeng I'd like to discuss this before we go and implement the injection. The canonical express way of accessing the app object is to use req.app from within the handler function. We are already doing this in loopback.rest().

    What is so special about your middleware that it needs the app object outside of the handler function?

  4. bajtos commented on Dec 17, 2014

    @bajtos
    MemberAuthor

    Looking at oauth2-loopback.js, the component needs the app object for two tasks:

    1. Register middleware prerequisites
      var session = (options.session !== false);
    
      app.middleware('auth:before', passport.initialize());
      if (session) {
        app.middleware('auth', passport.session());
      }
    1. Setup models
    var models = modelBuilder(app, options);

    Further inspection of models/index reveals following usage:

    • get hold of loopback to call loopback.getModelByType to auto-detect User and Application models to use
    • resolve dataSource from a name via app.dataSources[name]

    I would prefer to improve the middleware-registration process to support all three cases listed above instead of injecting the app object.

  5. bajtos commented on Jan 5, 2015

    @bajtos
    MemberAuthor

    On the second thought, middleware factory should not setup models nor register other middleware. Instead, we should implement a new mechanism to support complex components like oauth2.

    The component exports function(app, options), end-user configures components in a new file (component-config.json?) and loopback-boot will invoke the functions exported by configured components.

    This is most likely the item/feature that should have been implemented as part of #45.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions