Skip to content

Instantly share code, notes, and snippets.

@harshil93
Created June 8, 2015 07:11
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save harshil93/dea6c6061465dd74af94 to your computer and use it in GitHub Desktop.
Save harshil93/dea6c6061465dd74af94 to your computer and use it in GitHub Desktop.
Retrieving / Getting the current user id in a remote method in strongloop's loopback framework.
var loopback = require('loopback');
module.exports = function(SampleModel) {
// Returns null if the access token is not valid
function getCurrentUserId() {
var ctx = loopback.getCurrentContext();
var accessToken = ctx && ctx.get('accessToken');
var userId = accessToken && accessToken.userId;
return userId;
}
SampleModel.testRemoteMethod = function(cb){
var userId = getCurrentUserId();
if(!userId){
// Code if valid access token not found
}else{
// Code for authenticated users.
}
}
// Define testRemoteMethod as a remote method also
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment