Created
November 18, 2021 10:46
-
-
Save jscyo/840392aa50392e2bcc5a48fdd1f7304e to your computer and use it in GitHub Desktop.
Helper function for getting Auth0 users from exported data
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
let getEmailPasswordUserIfExistInAuth0 = async (email, password) => { | |
try { | |
let auth0UsersDataString = fs.readFileSync(__dirname + auth0ExportedUsersDataFile) | |
let auth0UsersData = JSON.parse(auth0UsersDataString); | |
// get auth0 user info | |
let auth0UsersInfo = auth0UsersData.userData | |
// get auth0 password hashes | |
let auth0PasswordHashes = auth0UsersData.passwordHashes | |
// check if input email has an associated user | |
for (let i = 0; i < auth0UsersInfo.length; i++) { | |
if ((!auth0UsersInfo[i].identities[0].isSocial) && auth0UsersInfo[i].email === email) { | |
// get password hash from associated userid | |
let usersPasswordHash = getPasswordHashFromUserId(auth0UsersInfo[i].identities[0].user_id, auth0PasswordHashes) | |
if (verifyPasswordhash(password, usersPasswordHash)) { | |
return auth0UsersInfo[i] | |
} | |
} | |
} | |
return undefined | |
} catch (error) { | |
console.log(error) | |
return undefined | |
} | |
} | |
let getSocialProviderUserIfExistInAuth0 = async (userId) => { | |
try { | |
let auth0UsersDataString = fs.readFileSync(__dirname + auth0ExportedUsersDataFile) | |
let auth0UsersData = JSON.parse(auth0UsersDataString); | |
// get auth0 user info | |
let auth0UsersInfo = auth0UsersData.userData | |
// get auth0 password hashes | |
let auth0PasswordHashes = auth0UsersData.passwordHashes | |
// check if input userId has an associated user which is social | |
for (let i = 0; i < auth0UsersInfo.length; i++) { | |
if ((auth0UsersInfo[i].identities[0].isSocial) && auth0UsersInfo[i].identities[0].user_id === userId) { | |
return auth0UsersInfo[i] | |
} | |
} | |
return undefined | |
} catch (error) { | |
console.log(error) | |
return undefined | |
} | |
} | |
function getPasswordHashFromUserId(userId, auth0PasswordHashes) { | |
for (let i = 0; i < auth0PasswordHashes.length; i++) { | |
if (auth0PasswordHashes[i]._id.$oid === userId) { | |
return auth0PasswordHashes[i].passwordHash | |
} | |
} | |
return undefined | |
} | |
function verifyPasswordhash(password, passwordHash) { | |
if (passwordHash === undefined) { | |
return false | |
} | |
return bcrypt.compareSync(password, passwordHash) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment