Created
June 8, 2015 07:11
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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