Saturday 6 May 2017

How to pass data from connect route to Meteor

I am adding an auth layer and I think I have it figured out except for one tricky detail. My Meteor app doesn't have any routes but I've added a hook into the connect middleware so that the "/" route errors if there isn't a correct API token. If the token is okay then I call next() to forward the route to Meteor.

The problem is that, depending on the token, I need to set server-side parameters for the connection, and I don't know how to do this. For example, say I have a static list of API keys mapped to permission levels. If a user sends a request with "ADMIN_API_KEY" then I would like to set Session.permission_level = "admin" for use by the Meteor server's functions. Session is just for the client in Meteor, though.

  # this code's in coffeescript

  WebApp.connectHandlers.use '/', (req, res, next) ->
    validator = new RequestValidator(req, next)
    validations = [
      "valid_namespace",
      "only_https"
    ]
    error = validator.validate(validations)
    next(error)
    # <<<<<<<<<<<<<<<<<<<<<<<<
    # Here I want to set some config option which can be
    # read by the server in the same way it can read things like
    # Meteor.user()

In Rails I would just say session[:permission_level] = "admin". But it seems to not work this way in Meteor.

By the way, I am not using a Routing package yet in Meteor, though if that would make this easier than I would.



via maxple

No comments:

Post a Comment