Created
June 18, 2020 16:37
-
-
Save maitham/d73565147f1177cd54f4ee7d96aca825 to your computer and use it in GitHub Desktop.
Auth0 callback
This file contains hidden or 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
async function (user, context, callback) { | |
user.app_metadata = user.app_metadata || {}; | |
if (user.app_metadata.signedup) { | |
return callback(null, user, context); | |
} | |
const fetch = require('[email protected]'); | |
var baseURL = configuration.BACKEND_URL; | |
var tokenSecret = configuration.BACKEND_SECRET; | |
var body; | |
if (context.connection === 'google-oauth2') { | |
body = { | |
"first_name": user.given_name, | |
"last_name": user.family_name, | |
"email": user.email, | |
"auth0_id": user.identities[0].user_id, | |
"avatar": user.picture | |
}; | |
} else if (context.connection === 'email-password') { | |
body = { | |
"email": user.email, | |
"auth0_id": user.identities[0].user_id, | |
"avatar": user.picture | |
}; | |
} | |
const response = await fetch(`${baseURL}api/v1/users/`, { | |
method: 'POST', | |
body: JSON.stringify(body), | |
headers: { | |
"Content-Type": "application/json", | |
"AuthToken": tokenSecret | |
}, | |
}); | |
const newUser = await response.json(); | |
if(!response.ok) return callback(null, user, context); | |
user.app_metadata.signedup = true; | |
user.app_metadata.uid = newUser.id; | |
console.log(newUser); | |
auth0.users.updateAppMetadata(user.user_id, user.app_metadata) | |
.then(function () { | |
callback(null, user, context); | |
}) | |
.catch(function (err) { | |
callback(err); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment