I am trying to add a v4 UUID to each user at login. This after finding this is not possible at registration.
My first stage was to get the value to populate. I ended with this:
//add-uuid-to-app-metadata Action
exports.onExecutePostLogin = async (event, api) => {
const { v4: uuidv4 } = require(‘uuid’);
event.user.app_metadata.uuid = event.user.app_metadata.uuid || uuidv4();
api.user.setAppMetadata(“uuid”, event.user.app_metadata.uuid)
};
This works, but of course it changes the uuid value in the metadata every time the same user logs in.
I found some examples that used an “if exist” type logic to not change the uuid if already there.
I have been working with the code below:
//add-uuid-to-app-metadata Action
exports.onExecutePostLogin = async (event, api) => {
const { v4: uuidv4 } = require(‘uuid’);
const { uuid } = event.user.user_metadata;
// short-circuit if the user signed up already
if ( uuid ) {
return;
}
// first time login/signup
api.user.setUserMetadata(“uuid”, uuidv4);
return;
};
My ‘if’ statement is not valid. I have tried variations and looked at other examples.
Could someone with more experienced than I with the scripting see what I need to make this work?
Any help is appreciated.