Last active
April 16, 2020 07:10
-
-
Save thameera/d9945ed704b66c6398359b9d2f6c0a2d to your computer and use it in GitHub Desktop.
Update metadata with a single patch call
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
function(user, context, callback) { | |
const request = require('request'); | |
const userApiUrl = auth0.baseUrl + '/users'; | |
function updateMetadata(cb) { | |
console.log('updating'); | |
request.patch({ | |
url: userApiUrl + '/' + user.user_id, | |
headers: { | |
Authorization: 'Bearer ' + auth0.accessToken | |
}, | |
json: { | |
app_metadata: user.app_metadata, | |
user_metadata: user.user_metadata | |
} | |
}, function(err, res, body) { | |
if (err) { | |
return cb(err); | |
} | |
if (res.statusCode >= 400) { | |
return cb(new Error('Error updating metadata: ' + res.statusMessage)); | |
} | |
return cb(null); | |
}); | |
} | |
user.user_metadata = { | |
color: 'red' | |
}; | |
let changed = true; | |
if (changed) { | |
return updateMetadata(function(err) { | |
callback(err, user, context); | |
}); | |
} | |
callback(null, user, context); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment