2

I am trying to make use of existing sample code for PassKit to create a JWT token with "Run JavaSript by Zapier". But the Zapier does not recognized the btoa function.

ReferenceError: btoa is not defined

According to this website, this function can be called directly. Any idea?

var b64data = btoa("this is my string to turn into base64");

Below is the code that I have written.

var Zap = {
  base64url:function(input){
    var base64String = btoa(input); //<--error here
    return urlConvertBase64(base64String);
  },
  urlConvertBase64:function(input){
    var output = input.replace(/=+$/, '');
    output = output.replace(/\+/g, '-');
    output = output.replace(/\//g, '_');
    return output;
  },
  generateJWT:function(key){
    var header = {
      "alg": "HS256",
      "typ": "JWT"
    };
    var time_now = Math.floor(new Date().getTime()/1000);
    var exp = time_now + 30;
    var body = {
      "exp": exp,
      "key": key
    };
    var token = [];
    token[0] = Zap.base64url(JSON.stringify(header));
    return body;
  }
};
output = [{body: Zap.generateJWT(inputData.Api_key)}]
Lzc
  • 21
  • 1
  • 2
  • 1
    Does this answer your question? [Node.js throws "btoa is not defined" error](https://stackoverflow.com/questions/23097928/node-js-throws-btoa-is-not-defined-error) – SMAKSS Jun 21 '20 at 15:46

1 Answers1

7

You can use the following:

var b64data = Buffer.from('this is my string to turn into base64').toString('base64');

Node.js doesn't seem to support the atob() and btoa() methods.

will-yama
  • 680
  • 5
  • 10