Last active
March 10, 2023 20:04
-
-
Save saltukalakus/cd20182ea1f9bcc4ad88a2cdd0316531 to your computer and use it in GitHub Desktop.
migrateUsersRule.js
This file contains 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 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