SimpleRest middleware for validating a Meteor.user's login token
This middleware can be accessed as:
JsonRoutes.Middleware.authenticateMeteorUserByToken
request.authToken
- String
- A valid login token for a
Meteor.user
account (requiresaccounts-base
)
request.userId
- String
- If the
request.authToken
is found in a user account, sets this to the ID of the authenticated user. Otherwise,null
.
Simply add this layer of middleware after any token parsing middleware, and voila!
For example:
JsonRoutes.Middleware.use('/auth', JsonRoutes.Middleware.parseBearerToken);
JsonRoutes.Middleware.use('/auth', JsonRoutes.Middleware.authenticateMeteorUserByToken);
JsonRoutes.add('GET', 'auth/test', function (request, response) {
// The authenticated user's ID will be set by this middleware
var userId = request.userId;
});