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.