Reputation: 437
I try to decode base64 encoded string token in react native, atob not work and library like js-base64 not resolve the problem.
Someone have a solution ?
Upvotes: 17
Views: 38649
Reputation: 9154
I find some simple way worked for me, the same api as node.
Install buffer
yarn add buffer
Usage:
console.log(Buffer.from("Hello World").toString('base64'));
console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString('ascii'));
Upvotes: 27
Reputation: 884
Another way is to install this react-native-base64 package and use it as below in react native.
npm install --save react-native-base64
import base64 from 'react-native-base64'
base64.encode('Some string to encode to base64');
base64.decode('VGhlIG51bWJlciBpcyA2MDAwMCwgd2hpY2ggY2FuIGJlIHJlYWQgYXMgc2l4dHkgdGhvdXNhbmQuJm5ic3A7PEJSPg==')
Upvotes: 4
Reputation: 1713
atob and btoa are not supported in JavascriptCore but works when the app runs under Chrome debugger, because JS code runs in Chrome when debugged. There are many base64 modules. https://github.com/mathiasbynens/base64 works fine for me.
Upvotes: 12