Skip to content

Instantly share code, notes, and snippets.

@jscyo
Last active September 28, 2022 16:28
Show Gist options
  • Save jscyo/dacb223a8553463a32ad8b6bb98f11fd to your computer and use it in GitHub Desktop.
Save jscyo/dacb223a8553463a32ad8b6bb98f11fd to your computer and use it in GitHub Desktop.
Helper functions for Auth0 to SuperTokens migration
let auth0Domain = "https://yourdomain.com"
let userIdMappingFile = "/userDataMapping.json"
let doesUserExistInAuth0DataBase = async (email, password) => {
let config = {
method: "post",
url: auth0Domain + "/oauth/token",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
data: {
client_id: "client id",// user app clientid
grant_type: "password",
username: email,
password: password,
}
};
try {
let access_token = (await axios(config)).data.access_token;
let userResponse = await axios.get(`${auth0Domain}/userInfo`, {
headers: { "authorization": `Bearer ${access_token}` },
})
return userResponse.data
} catch (error) {
if (error.response.status === 400 || error.response.status === 403) {
return undefined
}
throw error
}
}
let mapUserData = (auth0UserId, STUserId) => {
try {
let userIdMappingString = fs.readFileSync(__dirname + userIdMappingFile)
let userIdMapping = JSON.parse(userIdMappingString)
userIdMapping = { ...userIdMapping, [auth0UserId]: STUserId }
fs.writeFileSync(__dirname + userIdMappingFile, JSON.stringify(userIdMapping))
} catch (error) {
console.log(error)
}
}
let getSTUserIdFromAuth0UserId = (Auth0UserId) => {
try {
let userIdMappingString = fs.readFileSync(__dirname + userIdMappingFile)
let userIdMapping = JSON.parse(userIdMappingString)
if (userIdMapping[Auth0UserId] === undefined) {
return Auth0UserId
} else {
return userIdMapping[Auth0UserId]
}
} catch (error) {
return Auth0UserId
}
}
let getAuth0UserIdFromSTUserId = (STUserId) => {
try {
let userIdMappingString = fs.readFileSync(__dirname + userIdMappingFile)
let userIdMapping = JSON.parse(userIdMappingString)
let keys = Object.keys(userIdMapping)
for (let i = 0; i < keys.length; i++) {
if (userIdMapping[keys[i]] === STUserId) {
return keys[i]
}
}
return STUserId
} catch (error) {
return STUserId
}
}
async function doesThirdPartyUserIdExistInAuth0Database(userId) {
let config = {
method: 'POST',
url: `${auth0Domain}/oauth/token`,
headers: { 'content-type': 'application/json' },
data: '{"client_id":"sample_client_id","client_secret":"sample_client_secret","audience":"https://yourdomain/api/v2/","grant_type":"client_credentials"}'
};
try {
let access_token = (await axios(config)).data.access_token
let conf = {
method: 'GET',
url: `${auth0Domain}/api/v2/users`,
params: { q: `identities.user_id:"${userId}"` },
headers: { authorization: `Bearer ${access_token}` }
};
let response = await axios(conf)
return response.data[0].user_id
} catch (error) {
return undefined
}
}
module.exports = { doesUserExistInAuth0DataBase, mapUserData, getSTUserIdFromAuth0UserId, getAuth0UserIdFromSTUserId, doesThirdPartyUserIdExistInAuth0 }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment