-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #127 from auth0/linking-rules-fix
Linking rules fix
- Loading branch information
Showing
4 changed files
with
9 additions
and
13 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,7 +99,7 @@ | |
"access control" | ||
], | ||
"description": "<p>This rule will link any accounts that have the same email address while merging metadata.</p>\n", | ||
"code": "function (user, context, callback) {\n var request = require('[email protected]');\n var async = require('[email protected]');\n\n // Check if email is verified, we shouldn't automatically\n // merge accounts if this is not the case.\n if (!user.email_verified) {\n return callback(null, user, context);\n }\n var userApiUrl = auth0.baseUrl + '/users';\n\n request({\n url: userApiUrl,\n headers: {\n Authorization: 'Bearer ' + auth0.accessToken\n },\n qs: {\n search_engine: 'v2',\n q: 'email.raw:\"' + user.email + '\" AND email_verified: \"true\" -user_id:\"' + user.user_id + '\"',\n }\n },\n function(err, response, body) {\n if (err) return callback(err);\n if (response.statusCode !== 200) return callback(new Error(body));\n\n var data = JSON.parse(body);\n if (data.length > 1) {\n return callback(new Error('[!] Rule: Multiple user profiles already exist - cannot select base profile to link with'));\n }\n if (data.length === 0) {\n console.log('[-] Skipping link rule');\n return callback(null, user, context);\n }\n\n var originalUser = data[0];\n var user_id = user.user_id;\n var pipePos = user_id.indexOf('|');\n var provider = user_id.slice(0, pipePos);\n var newUserId = user_id.slice(pipePos + 1);\n\n user.app_metadata = user.app_metadata || {};\n user.user_metadata = user.user_metadata || {};\n auth0.users.updateAppMetadata(originalUser.user_id, user.app_metadata)\n .then(auth0.users.updateUserMetadata(originalUser.user_id, user.user_metadata))\n .then(function(){\n request.post({\n url: userApiUrl + '/' + originalUser.user_id + '/identities',\n headers: {\n Authorization: 'Bearer ' + auth0.accessToken\n },\n json: { provider: provider, user_id: newUserId }\n }, function(err, response, body) {\n if (response && response.statusCode >= 400) {\n return cb(new Error('Error linking account: ' + response.statusMessage));\n }\n context.primaryUser = originalUser.user_id;\n callback(null, user, context);\n });\n })\n .catch(function(err){\n cb(err);\n });\n });\n}" | ||
"code": "function (user, context, callback) {\n var request = require('[email protected]');\n var async = require('[email protected]');\n\n // Check if email is verified, we shouldn't automatically\n // merge accounts if this is not the case.\n if (!user.email_verified) {\n return callback(null, user, context);\n }\n var userApiUrl = auth0.baseUrl + '/users';\n\n request({\n url: userApiUrl,\n headers: {\n Authorization: 'Bearer ' + auth0.accessToken\n },\n qs: {\n search_engine: 'v2',\n q: 'email.raw:\"' + user.email + '\" AND email_verified: \"true\" -user_id:\"' + user.user_id + '\"',\n }\n },\n function(err, response, body) {\n if (err) return callback(err);\n if (response.statusCode !== 200) return callback(new Error(body));\n\n var data = JSON.parse(body);\n if (data.length > 1) {\n return callback(new Error('[!] Rule: Multiple user profiles already exist - cannot select base profile to link with'));\n }\n if (data.length === 0) {\n console.log('[-] Skipping link rule');\n return callback(null, user, context);\n }\n\n var originalUser = data[0];\n var provider = user.identities[0].provider;\n var providerUserId = user.identities[0].user_id;\n\n user.app_metadata = user.app_metadata || {};\n user.user_metadata = user.user_metadata || {};\n auth0.users.updateAppMetadata(originalUser.user_id, user.app_metadata)\n .then(auth0.users.updateUserMetadata(originalUser.user_id, user.user_metadata))\n .then(function(){\n request.post({\n url: userApiUrl + '/' + originalUser.user_id + '/identities',\n headers: {\n Authorization: 'Bearer ' + auth0.accessToken\n },\n json: { provider: provider, user_id: providerUserId }\n }, function(err, response, body) {\n if (response && response.statusCode >= 400) {\n return cb(new Error('Error linking account: ' + response.statusMessage));\n }\n context.primaryUser = originalUser.user_id;\n callback(null, user, context);\n });\n })\n .catch(function(err){\n cb(err);\n });\n });\n}" | ||
}, | ||
{ | ||
"id": "link-users-by-email", | ||
|
@@ -108,7 +108,7 @@ | |
"access control" | ||
], | ||
"description": "<p>This rule will link any accounts that have the same email address.</p>\n<blockquote>\n<p>Note: When linking accounts, only the metadata of the target user is saved. If you want to merge the metadata of the two accounts you must do that manually. See the document on <a href=\"https://auth0.com/docs/link-accounts\">Linking Accounts</a> for more details.</p>\n</blockquote>\n", | ||
"code": "function (user, context, callback) {\n var request = require('[email protected]');\n // Check if email is verified, we shouldn't automatically\n // merge accounts if this is not the case.\n if (!user.email_verified) {\n return callback(null, user, context);\n }\n var userApiUrl = auth0.baseUrl + '/users';\n\n request({\n url: userApiUrl,\n headers: {\n Authorization: 'Bearer ' + auth0.accessToken\n },\n qs: {\n search_engine: 'v2',\n q: 'email.raw:\"' + user.email + '\" AND email_verified: \"true\" -user_id:\"' + user.user_id + '\"',\n }\n },\n function(err, response, body) {\n if (err) return callback(err);\n if (response.statusCode !== 200) return callback(new Error(body));\n\n var data = JSON.parse(body);\n if (data.length > 1) {\n return callback(new Error('[!] Rule: Multiple user profiles already exist - cannot select base profile to link with'));\n }\n if (data.length === 0) {\n console.log('[-] Skipping link rule');\n return callback(null, user, context);\n }\n\n var originalUser = data[0];\n var user_id = user.user_id;\n var pipePos = user_id.indexOf('|');\n var provider = user_id.slice(0, pipePos);\n var newUserId = user_id.slice(pipePos + 1);\n\n request.post({\n url: userApiUrl + '/' + originalUser.user_id + '/identities',\n headers: {\n Authorization: 'Bearer ' + auth0.accessToken\n },\n json: {\n provider: provider,\n user_id: newUserId\n }\n }, function(err, response, body) {\n if (response.statusCode >= 400) {\n return callback(new Error('Error linking account: ' + response.statusMessage));\n }\n context.primaryUser = originalUser.user_id;\n callback(null, user, context);\n });\n });\n}" | ||
"code": "function (user, context, callback) {\n var request = require('[email protected]');\n // Check if email is verified, we shouldn't automatically\n // merge accounts if this is not the case.\n if (!user.email_verified) {\n return callback(null, user, context);\n }\n var userApiUrl = auth0.baseUrl + '/users';\n\n request({\n url: userApiUrl,\n headers: {\n Authorization: 'Bearer ' + auth0.accessToken\n },\n qs: {\n search_engine: 'v2',\n q: 'email.raw:\"' + user.email + '\" AND email_verified: \"true\" -user_id:\"' + user.user_id + '\"',\n }\n },\n function(err, response, body) {\n if (err) return callback(err);\n if (response.statusCode !== 200) return callback(new Error(body));\n\n var data = JSON.parse(body);\n if (data.length > 1) {\n return callback(new Error('[!] Rule: Multiple user profiles already exist - cannot select base profile to link with'));\n }\n if (data.length === 0) {\n console.log('[-] Skipping link rule');\n return callback(null, user, context);\n }\n\n var originalUser = data[0];\n var provider = user.identities[0].provider;\n var providerUserId = user.identities[0].user_id;\n\n request.post({\n url: userApiUrl + '/' + originalUser.user_id + '/identities',\n headers: {\n Authorization: 'Bearer ' + auth0.accessToken\n },\n json: {\n provider: provider,\n user_id: providerUserId\n }\n }, function(err, response, body) {\n if (response.statusCode >= 400) {\n return callback(new Error('Error linking account: ' + response.statusMessage));\n }\n context.primaryUser = originalUser.user_id;\n callback(null, user, context);\n });\n });\n}" | ||
}, | ||
{ | ||
"id": "roles-creation", | ||
|
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
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