Skip to content

Instantly share code, notes, and snippets.

@pulkitsinghal
Last active January 5, 2016 13:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pulkitsinghal/37c10c30a1a13e9f1db8 to your computer and use it in GitHub Desktop.
Save pulkitsinghal/37c10c30a1a13e9f1db8 to your computer and use it in GitHub Desktop.
Fetch user for remote methods in loopback v2.8.5
var loopback = require('loopback');
module.exports = function(MyModel) {
//remote method
MyModel.importProducts = function(storeConfigId, storeId, cb) {
//cb(null, sound + ' ' + sound + ' ' + sound);
var ctx = loopback.getCurrentContext();
console.log(loopback.getCurrentContext());
var accessToken = ctx.get('accessToken');
console.log('accessToken: ', accessToken);
var currentUser = ctx && ctx.get('currentUser');
console.log('currentUser: ', currentUser);
/*var ctx = loopback.getCurrentContext();
var token = ctx.get('accessToken');
if ( ! token) {
cb('token not set');
}
else {
MyModel.app.models.UserModel.findById(token.userId, function(err, user) {
if (err) return cb(err);
if ( ! user) return cb(new Error('No user with this access token was found.'));
console.log(user);
cb(null);
});
}*/
cb(null);
};
MyModel.remoteMethod(
'hello',
{
http: {path:'/hello', verb: 'get'}
}
);
};
{
"name": "UserModel",
"base": "User",
"idInjection": true,
"properties": {},
"validations": [],
"relations": {
...
},
"acls": [...],
"methods": []
}
var loopback = require('loopback');
var boot = require('loopback-boot');
var app = module.exports = loopback(); // TODO: how to access this globally in model.js files too?
// Set up the /favicon.ico
app.use(loopback.favicon());
// request pre-processing middleware
app.use(loopback.compress());
// -- Add your pre-processing middleware here --
//app.use(loopback.context()); // Commented because: Enable the context middleware from loopback.rest (Raymond Feng)
app.use(loopback.token());
app.use(function (req, res, next) {
if (!req.accessToken) {
console.log('no accesstoken found on request');
return next();
}
app.models.UserModel.findById(req.accessToken.userId, function(err, user) {
if (err) {
return next(err);
}
if (!user) {
return next(new Error('No user with this access token was found.'));
}
console.log('user: ', user);
//res.locals.currentUser = user;
var loopbackContext = loopback.getCurrentContext();
if (loopbackContext) {
console.log('setting currentUser');
loopbackContext.set('currentUser', user);
}
next();
});
});
// boot scripts mount components like REST API
boot(app, __dirname);
// -- Mount static files here--
// All static middleware should be registered at the end, as all requests
// passing the static middleware are hitting the file system
// Example:
var path = require('path');
//app.use(loopback.static(path.resolve(__dirname, '../client')));
app.use(loopback.static(path.join(__dirname, '../client/.tmp')));
app.use(loopback.static(path.join(__dirname, '../client/app')));
// Requests that get this far won't be handled
// by any middleware. Convert them into a 404 error
// that will be handled later down the chain.
app.use(loopback.urlNotFound());
// The ultimate error handler.
app.use(loopback.errorHandler());
app.start = function() {
// start the web server
return app.listen(function() {
app.emit('started');
console.log('Web server listening at: %s', app.get('url'));
});
};
// start the server if `$ node server.js`
if (require.main === module) {
app.start();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment