I’m using LinkedIn Social / OAuth login to register and sign-in users with Auth0. On their first log-in (registration) I want an action to create a “user” object/document in FaunaDB using the Auth0 “event.user” JSON object that was created from the data passed by LinkedIn. I wrote the script for it in the action’s part of post-registration-flow, tested it with the FaunaDB secret key (it worked great), deployed it, set up the action flow, and it’s just not passing the new user’s info to FaunaDB after a user’s first login using LinkedIn Social Sign In.
Is there an error log for actions that I can view? Is LinkedIn social login technically not considered a registration even though user member is created in Auth0’s “User Management” tab? Am I messing up the Auth0 Actions configuration? I’d greatly appreciate any insight. Thanks so much, everyone!
/**
* Handler that will be called during the execution of a PostUserRegistration flow.
*
* @param {Event} event - Details about the context and user that has registered.
*/
const axios = require('axios');
exports.onExecutePostUserRegistration = async (event) => {
const given_name = event.user.given_name;
const family_name = event.user.family_name;
const nickname = event.user.nickname;
const name = event.user.name;
const picture = event.user.picture;
const updated_at = event.user.updated_at;
const email = event.user.email;
const emailed_verified = event.user.emailed_verified;
const sub = event.user.user_id;
const variables = { given_name, family_name, nickname, name, picture, updated_at, email, emailed_verified, sub };
const CREATE_USER = `
mutation(
$given_name: String,
$family_name: String,
$nickname: String,
$name: String,
$picture: String,
$updated_at: String,
$email: String,
$emailed_verified: Boolean,
$sub: String) {
createUser(
data: {
given_name: $given_name,
family_name: $family_name,
nickname: $nickname,
name: $name,
picture: $picture,
updated_at: $updated_at,
email: $email,
emailed_verified: $emailed_verified,
sub: $sub
}
) {
_id
_ts
}
}
`
const sendQuery = async (query, variables) => {
const {
data: { data, errors },
} = await axios({
url: 'https://graphql.us.fauna.com/graphql',
method: 'POST',
headers: {
Authorization: `Bearer ${event.secrets.FAUNA_SECRET_KEY}`,
},
data: {
query,
variables,
},
});
if (errors) {
console.error(errors);
throw new Error('Something went wrong on sendQuery');
}
return data;
};
const formattedResponse = (statusCode, body) => {
return {
statusCode,
body: JSON.stringify(body),
};
};
try {
const { createUser } = await sendQuery(
CREATE_USER,
variables
);
return formattedResponse(200, createUser);
} catch (err) {
console.error(err);
return formattedResponse(500, { err: 'There was an error in creating a new user.' });
}
};