How can I get the response from a post request in Method.create to the client-side?

I have been struggling to get from a post request a token, to authenticate a plugin on the client-side. I managed it to work by calling the authentication on the client-side but this is not secure so I am trying to make it work on the server-side.

On the client-side, I wrote

Template.stripoCreateEmail.onCreated(function () {
    Meteor.call('stripoPlugin', (error, token) => {
       console.log(token);
       loadDemoTemplate(initPlugin, token);
    });
});

and on the server

Method.create('stripoPlugin', function () {
    request.post('https://plugins.stripo.email/api/v1/auth', {
        json: {
            pluginId: 'Plugin_Key',
            secretKey: 'Secrate_Key'
        }
    }, (error, res, body) => {
        if (error) {
            console.error(error)
            return
        }
        console.log(`statusCode: ${res.statusCode}`);
        console.log('token: ' + body.token);
        token = body.token; // How can I get that token to the client side?
      
    });
});
  1. You could embed your statement request.post(...) into a Promise, and return that instance in the Method. Add
    return resolve(token);
    …and replace console.error(...) with…
    return reject(error); respectively. Meteor server will wait for your Promise’s completion and deliver the result to the client.

  2. Alternatively, see the section Promises & Async/Await (request) so as to see how you can make request to return a Promise on its own. Then you don’t need to write the Promise wrapper yourself, see #1

  3. Alternatively, you can also use Meteor’s own synchronous HTTP.call(), see the doc here.

Note that the npm package request is deprecated. You might want to replace it with something else—I personally favor axios.