Skip to content

Instantly share code, notes, and snippets.

@maitham
Created June 18, 2020 16:37
Show Gist options
  • Save maitham/d73565147f1177cd54f4ee7d96aca825 to your computer and use it in GitHub Desktop.
Save maitham/d73565147f1177cd54f4ee7d96aca825 to your computer and use it in GitHub Desktop.
Auth0 callback
async function (user, context, callback) {
user.app_metadata = user.app_metadata || {};
if (user.app_metadata.signedup) {
return callback(null, user, context);
}
const fetch = require('isomorphic-fetch@2.2.0');
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