Pre-Registration Hook

I am having issues with the synchronous pre-registration hook. Any advise is highly appreciated.
The context is as follows:

  1. When I SignUp I want to leverage the pre-registration hook
  2. The hook should call a GET Rest-API (tried it with the got library)
  3. According to the response of the REST-API I have to return the proper callback for AUTH0

ISSUE:

REST Calls are asynchronous BUT the pre-registration has to be synchronous. How to achieve to synchronously call the REST Backend ?

This synchronous approach of the signup should be otherwise revised.

Current code example.

const got = require(‘got’);

module.exports = function (user, context, cb) {
var response = {};

response.user = user;

async function test () {

await Promise.all([got(‘https://apidev.coshare.ch/api/config’)]).then(
values => {
var result = values[0].body;

response.user.user_metadata.coshare = result;
console.log(“RESULT NEW”);
console.log(response);
cb(null, response);
}
);
// Add user or app metadata to the newly created user
// response.user.user_metadata = { foo: ‘bar’ };
// response.user.app_metadata = { vip: true, score: 7 };

// Deny the user’s registration and send a localized message to New Universal Login
// if (denyRegistration) {
// const LOCALIZED_MESSAGES = {
// en: ‘You are not allowed to register.’,
// es: ‘No tienes permitido registrarte.’
// };
//
// const localizedMessage = LOCALIZED_MESSAGES[context.renderLanguage] || LOCALIZED_MESSAGES[‘en’];
// return cb(new PreUserRegistrationError(‘Denied user registration in Pre User Registration Hook’, localizedMessage));
// }

console.log(“DONE”)
}

test();
};

Hi @kai.schwidder,

Welcome to the Community!

Just to clarify, could you talk a little bit more about what you’d like to use the pre-registration hook for?
Are you finding that the callback (cb) being called before the response is received?

I think you may need to update the async/await code a little:

const got = require('got');

module.exports = async function (user, context, cb) {
  var response = {};

  response.user = user;
  try {
    var values = await got('https://apidev.coshare.ch/api/config');
    var result = JSON.parse(values.body);
    response.user.user_metadata.coshare = result;
    console.log('RESULT NEW');
    console.log(user);
    cb(null, user, context);
  } catch (e) {
    console.log(e)
  }
};

You may find this FAQ helpful as well: Is it possible to use async await in rules?

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.