I have an auth0 rule that is meant to modify the data returned to the user. It is meant to add a user_metadata field. I tried doing that with this rule
async function loginFaunaOnUserLogin(user, context, callback) {
const faunadb = require("faunadb@2.11.1");
user.user_metadata = user.user_metadata || {};
const q = faunadb.query;
const client = new faunadb.Client({
secret: "",
});
try {
const user_from_fauna = await client.query(
q.Map(
q.Paginate(q.Match(q.Index("users_by_email"), user.email)),
q.Lambda("ref", q.Get(q.Var("ref")))
)
);
if (!user_from_fauna.data[0]) {
throw new Error("No user with this email exists");
}
const role = user_from_fauna.data[0].data.role;
const collection = `${role[0].toUpperCase()}${role.substr(1)}s`;
const index = `${role}s_by_email`;
const user_in_collection = await client.query(
q.Paginate(
q.Match(
q.Index(index),
user.email
)
)
);
console.log({user_in_collection});
let ref_id;
if (!user_in_collection.data[0]) {
const clientDetails = await client.query(
q.Create(q.Collection(collection), {
data: {
metadata: user_from_fauna.data[0].data,
},
credentials: {
password: user.user_id,
},
})
);
ref_id = clientDetails.ref.id;
} else {
ref_id = user_in_collection.data[0].id;
}
// Login Process
const credential = await client.query(
q.Login(q.Ref(q.Collection(String(collection)), ref_id), {
password: user.user_id,
ttl: q.TimeAdd(q.Now(), 10, "hour"),
})
);
user.user_metadata= {
...user_from_fauna.data[0].data, token: credential.secret, user_id: credential.instance.id
};
callback(null, user, context);
} catch (err) {
callback(err, user, context);
}
}
But it wasn’t successful. I could only successfully modify the nickname field to add the metadata. Like this
...
// shortened code
// Login Process
const credential = await client.query(
q.Login(q.Ref(q.Collection(String(collection)), ref_id), {
password: user.user_id,
ttl: q.TimeAdd(q.Now(), 10, "hour"),
})
);
user.nickname = {
...user_from_fauna.data[0].data, token: credential.secret, user_id: credential.instance.id
};
callback(null, user, context);
} catch (err) {
callback(err, user, context);
}
}
This is the schema of what is returned to the user
email: "",
email_verified: false
name: ""
nickname: {},
picture: "",
sub: ""
updated_at: ""