Skip to content

Instantly share code, notes, and snippets.

@justinspradlin
Last active December 21, 2015 16:18
Show Gist options
  • Save justinspradlin/6332107 to your computer and use it in GitHub Desktop.
Save justinspradlin/6332107 to your computer and use it in GitHub Desktop.
Azure Mobile Service Update User Info Custom Api
exports.post = function(request, response) {
var identities = request.user.getIdentities();
var basicProfileUrl;
if (identities.microsoft) {
var liveAccessToken = identities.microsoft.accessToken;
basicProfileUrl = 'https://apis.live.net/v5.0/me/?method=GET&access_token=' + liveAccessToken;
}
if (basicProfileUrl) {
var requestCallback = function (err, resp, body) {
if (err || resp.statusCode !== 200) {
console.error('Error getting basic profile data from the identity provider: ', err);
request.respond(statusCodes.INTERNAL_SERVER_ERROR, body);
} else {
try {
// Get the user data
var userData = JSON.parse(body);
console.log("Received data: %j", userData);
// Insert/Update data into the User table
var usersTable = request.service.tables.getTable('User');
usersTable
.where({ userId: request.user.userId})
.take(1)
.read({
success: function(results){
// Grab the existing data, else create a new one
var user = null;
var profileImageUrl = "https://apis.live.net/v5.0/" + userData.id + "/picture";
if(results.length > 0){
// Update the user record
user = results[0];
// Apply the values from the userData to the user
user.displayName = userData.name;
user.firstName = userData.first_name;
user.lastName = userData.last_name;
user.profileImageUrl = profileImageUrl;
// Persist the updated user object
console.log('Trying to update user: %j', user);
usersTable.update(user, {
success: function(){
response.send(statusCodes.OK, 'User data was successfully updated.');
},
error: function(err){
request.respond(statusCodes.INTERNAL_SERVER_ERROR, err);
}
});
}else{
user = {
userId: request.user.userId,
displayName: userData.name,
firstName: userData.first_name,
lastName: userData.last_name,
profileImageUrl: profileImageUrl
};
// Persist the new user object
console.log('Trying to insert user: %j', user);
usersTable.insert(user, {
success: function(){
response.send(statusCodes.OK, 'User data was successfully inserted.');
},
error: function(err){
request.respond(statusCodes.INTERNAL_SERVER_ERROR, err);
}
});
}
},
error: function(err){
console.log('Error reading User table. ' + err);
request.respond(statusCodes.INTERNAL_SERVER_ERROR, err);
}
});
} catch (ex) {
console.error('Error parsing response from the provider API: ', ex);
request.respond(statusCodes.INTERNAL_SERVER_ERROR, ex);
}
}
}
// Get the request module
var req = require('request');
// Build the http request to get user data
var reqOptions = {
uri: basicProfileUrl,
headers: { Accept: "application/json" }
};
// Perform the request for the user data
req(reqOptions, requestCallback);
}else{
response.send(statusCodes.OK, { message : 'No data update. Provider url was not found.' });
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment