API call in Pre User Registration hook

I’m trying to call our backend to get a Bearer Token in a Pre User Registration Hook in order to have access to it in my Apollo Client instance without making multiple calls and/or redirects.

module.exports = function (user, context, cb) {
  var response = {};
  var { request } = require('graphql-request');
  var API = "https://ourGraphqlEndpint/api";
  var query = `mutation {
     createUser(data: { email: $email, password: $password, username: $username }) {
        token
        user {
          id
        }
      }
      `;
      
  var variables = {
    email: user.email,
    password: user.password,
    username: user.username
  };
  
  var data= request(API, query, variables)
  .then(res => res.json())
  .then(data => {
    console.log(data)
    return data
  });

  response.user = user;
  response.user.user_metadata = { token: data.token }
 response.user.user_metadata = { our_id: data.id }

  cb(null, response);
};

This is my error

{
  "message": "Error when JSON serializing the result of the extension point",
  "statusCode": 500,
  "stack": "Error: Error when JSON serializing the result of the extension point\n    at buildResponse (/data/sandbox/node_modules/auth0-ext-compilers/lib/adapter.js:115:41)\n    at func (/data/sandbox/node_modules/auth0-ext-compilers/lib/compilers/user-registration.js:31:20)\n    at module.exports (/data/io/d55b3914-58a5-4438-b5da-d3ba53cd1f5a/webtask.js:49:3)\n    at Authz.is_authorized.error (/data/sandbox/node_modules/auth0-ext-compilers/lib/compilers/user-registration.js:30:16)\n    at Object.is_authorized (/data/sandbox/node_modules/auth0-ext-compilers/lib/authorization.js:13:81)\n    at userRegistrationHandler (/data/sandbox/node_modules/auth0-ext-compilers/lib/compilers/user-registration.js:9:18)\n    at parseBody (/data/sandbox/node_modules/auth0-ext-compilers/lib/adapter.js:90:20)\n    at finish (/data/sandbox/node_modules/auth0-ext-compilers/node_modules/wreck/lib/index.js:369:16)\n    at wrapped (/data/sandbox/node_modules/auth0-ext-compilers/node_modules/hoek/lib/index.js:879:20)\n    at module.exports.internals.Recorder.onReaderFinish (/data/sandbox/node_modules/auth0-ext-compilers/node_modules/wreck/lib/index.js:415:16)"
}

I’ve tried res.json and JSON.parse(res), but those are both DEserialization methods, so I’m not sure what to do about this. I’m also not sure if this is correct way to handle promises in this context.

My query wasn’t properly structured, and I’ve fixed that

  var query = `mutation CREATE_USER($name: String!, $email: String!, $password: String!) {
      createUser(data: { name: $name, email: $email, password: $password }) {
        token
        user {
          id
          name
        }
      }
    }
      `;

And I am having problems with asnyc/promises but I will post that in a different question

1 Like

Figured it out. This was my first attempt at using the auth0 hooks, so I’m going to leave the working snippet here in case it can help out someone else. Although, I’m pretty sure that’s not acceptable error handling in the catch block.

module.exports = function (user, context, cb) {
  var response = {};
  var { request } = require('graphql-request');
  var API = "https://graphql.endpoint/api";
  var query = `mutation CREATE_USER($name: String!, $email: String!, $password: String!) {
      createUser(data: { name: $name, email: $email, password: $password }) {
        token
        user {
          id
          name
        }
      }
    }
      `;
      
  var variables = {
    email: user.email,
    password: user.password,
    name: user.username
  };
  
  response.user = user;
  
  async function createOurUser () {
    let newUser;

    try {
      newUser = await request(API, query, variables);

      var { createUser : { token, user: { name }}} = newUser;

      response.user.user_metadata.token = { token: token }
      response.user.user_metadata.name = { name: name }

      cb(null, response);

    } catch (err) {
      console.log(err)
    }

  }
  
  createOurUser()
};
3 Likes

Glad you have figured it out and thanks for sharing with the rest of community!