Decoding base64 string returns undefined

Enonic version: 6.14.x
OS: Mac OS 10.13.6

Hi

I want to decode the jwt token by using the base64 decode function from the lib-text-encoding library, but it returns “undefined”. I checked the base64 string here: https://www.base64convert.com/decode, and it decodes it fine there.

This is the base64 string i try to decode:
eyJodHRwczovL2FwaS5jYW1iaS5jb20vcm9sZXMiOlsiYWRtaW4iLCJjYW1iaSJdLCJodHRwczovL2FwaS5jYW1iaS5jb20vZ3JvdXBzIjpbImFkbWluIiwidmlnbyJdLCJuaWNrbmFtZSI6Im95dmluZC5kYWhsIiwibmFtZSI6Im95dmluZC5kYWhsQGl0ZW0ubm8iLCJwaWN0dXJlIjoiaHR0cHM6Ly9zLmdyYXZhdGFyLmNvbS9hdmF0YXIvYWNkNjQzYWVkOTcyMmFhM2VhZWU0ZDI2ZmUwYzZmYmQ/cz00ODAmcj1wZyZkPWh0dHBzJTNBJTJGJTJGY2RuLmF1dGgwLmNvbSUyRmF2YXRhcnMlMkZveS5wbmciLCJ1cGRhdGVkX2F0IjoiMjAxOC0wOS0wNlQwODoxNDozOC42ODJaIiwiZW1haWwiOiJveXZpbmQuZGFobEBpdGVtLm5vIiwiZW1haWxfdmVyaWZpZWQiOnRydWUsImlzcyI6Imh0dHBzOi8vY2FtYmkuZXUuYXV0aDAuY29tLyIsInN1YiI6ImF1dGgwfDVhYTE0NmVhOTZjZGI3MTA5ZGRiMmNhMiIsImF1ZCI6Ik5RcEFxd2lCYmkwWklBaVphaVllOHJqN05DWVhPS0kzIiwiaWF0IjoxNTM2MjIxNjgxLCJleHAiOjE1MzYyNTc2ODF9

This is how I try to decode it:

var base64 = "eyJodHRwcz.... " // same as the text above
var encodingLib = require('/lib/text-encoding');
var decoded = encodingLib.base64Decode(base64);
log.info(decoded); // prints "undefined" 

Any tips on why I’m getting “undefined” ?

1 Like

I just tried your code in a main.js of an app to test it, and it works for me.

It prints "ByteSource.wrap(7B2268747470733A2F2F6170692...)" which is the toString() output of the binary object you get back.

What is strange is that if the value is not a valid base64 string, what it returns and prints is "null" , not "undefined".
Do you have another variable defined as decoded in that file?

1 Like

I guess I lied in the question. I did this to log the output:

log.info("%s", JSON.stringify(decoded, null, 2));

And JSON.stringify gave me “undefined”. When logging it without stringifying it gave me the same result as what you got.

Thanks a lot for the up clearance :slight_smile:

2 Likes