Skip to content

Instantly share code, notes, and snippets.

@saltukalakus
Last active March 10, 2023 20:04
Show Gist options
  • Save saltukalakus/cd20182ea1f9bcc4ad88a2cdd0316531 to your computer and use it in GitHub Desktop.
Save saltukalakus/cd20182ea1f9bcc4ad88a2cdd0316531 to your computer and use it in GitHub Desktop.
migrateUsersRule.js
function migrateUsers(user, context, cb) {
if (needMigration(user)) {
// Ignoring how the values are retrieved from the legacy database
var legacyProfile = {
family_name: 'alakus',
given_name: 'saltuk',
user_metadata: {
'anotherMetadata' : '123'
},
app_metadata: {
'migrated':true,
'userId':'xyz'
}
};
var ManagementClient = require('[email protected]').ManagementClient;
var management = new ManagementClient({
domain: auth0.domain,
token: auth0.accessToken
});
management.updateUser(
{ id: user.user_id },
legacyProfile,
function (err, updatedUser) {
if (err) {
cb(err);
} else {
updateRuleUser(user, updatedUser);
cb(null, user, context);
}
}
);
} else {
cb(null, user, context);
}
function needMigration(user) {
if (!user.app_metadata || (user.app_metadata && !user.app_metadata.migrated)) {
return true;
}
return false;
}
function updateRuleUser(user, updatedUser) {
for (var key in updatedUser) {
user[key] = updatedUser[key];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment