最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JWT decoding working in React Native but not for React - Stack Overflow

programmeradmin1浏览0评论

So when you decode the following in / it says it has a header named 'token' which has the token when you re-decode it you can access the payload.

Normally in RN I can access the payload containing token using this.props.user.token but in React the 'token' property is not there as it is not an javascript object but a string but I use the same API to my backend.

eyJ0b2tlbiI6ImV5SmhiR2NpT2lKSVV6STFOaUlzSW5SNWNDSTZJa3BYVkNKOS5leUpwWkhCaGMzTWlPaUl6WkRFME1ERmlNQzFtTkdWakxURXhaVGN0WVdReE15MHpaRGRsTUdZME9EUmlaR0k2TVRsaU4yTmpNREUyWVRVeU1ERTRNemt3TmpsbU56aGxNV0pqTXpBMk4yUTBZalJqTVRrNE1HTTBPV05tWkRsak9XWTRNV1F4Tm1VMllUbGhaR1JrTVNJc0ltbGhkQ0k2TVRVeE5UY3lNalF6TkgwLlg0dGUxRmhzbGJlcW5Id3RCQ2NOMHF5Zk9vb2tvdVN2TXRlLVBUTUtrMHcifQ==

I tried to decode it using

var decoded = jwt.decode(response.data);

but decoded is null, what can I do?

So when you decode the following in https://jwt.io/ it says it has a header named 'token' which has the token when you re-decode it you can access the payload.

Normally in RN I can access the payload containing token using this.props.user.token but in React the 'token' property is not there as it is not an javascript object but a string but I use the same API to my backend.

eyJ0b2tlbiI6ImV5SmhiR2NpT2lKSVV6STFOaUlzSW5SNWNDSTZJa3BYVkNKOS5leUpwWkhCaGMzTWlPaUl6WkRFME1ERmlNQzFtTkdWakxURXhaVGN0WVdReE15MHpaRGRsTUdZME9EUmlaR0k2TVRsaU4yTmpNREUyWVRVeU1ERTRNemt3TmpsbU56aGxNV0pqTXpBMk4yUTBZalJqTVRrNE1HTTBPV05tWkRsak9XWTRNV1F4Tm1VMllUbGhaR1JrTVNJc0ltbGhkQ0k2TVRVeE5UY3lNalF6TkgwLlg0dGUxRmhzbGJlcW5Id3RCQ2NOMHF5Zk9vb2tvdVN2TXRlLVBUTUtrMHcifQ==

I tried to decode it using

var decoded = jwt.decode(response.data);

but decoded is null, what can I do?

Share Improve this question edited Jan 12, 2018 at 2:59 Leo asked Jan 12, 2018 at 2:49 LeoLeo 911 gold badge2 silver badges7 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 16

You can use any npm module for decoding like :

npm install jwt-decode

var jwtDecode = require('jwt-decode');

var token = 'eyJ0eXAiO.../// jwt token';

var decoded = jwtDecode(token);
console.log(decoded);

For further info check here

It's work well:

var jwtDecode = require('jwt-decode');

var decoded = jwtDecode(testString, { body: true });

import { Buffer } from "buffer"

const token = 'xxx.xxx.xxx';

const parts: Buffer[] = token.split('.').map((part: string): Buffer => {
            return Buffer.from(part.replace(/-/g, '+').replace(/_/g, '/'), 'base64');
        });
const payload = JSON.parse(parts[1].toString());
发布评论

评论列表(0)

  1. 暂无评论