Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

local-storage - 要解码的字符串未正确编码,编码由JsonWebtoken完成(The string to be decoded is not correctly encoded, Encoding done by JsonWebtoken)

which way is the correct one for decoding tokens in the client side that are encoded using the node jsonwebtoken module, this is how am creating the token

(哪种方法是用于解码使用节点jsonwebtoken模块编码的客户端中的令牌的正确方法,这就是创建令牌的方式)

let token = jwt.sign({
   my_data: data,
   exp: parseInt(expiry.getTime() / 1000) 
}, "SECRET"); 

res.status(200).json({jwt_token: token });

And in the client side, I save the token using localStorage like:

(在客户端,我使用localStorage保存令牌,例如:)

let token = server_response_token;
window.localStorage.setItem('client_token', token );

And then I try to decode the token from the local storage like:

(然后,我尝试从本地存储中解码令牌,例如:)

let payload = window.localStorage.getItem('client_token').split('.')[1];
let decoded = window.atob(payload);

But with that I am getting an error:

(但是与此同时我得到一个错误:)

Uncaught DOMException: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded.

Where am I going wrong here, thanks.

(我在哪里错了,谢谢。)

  ask by Teebo translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need to use of atob() function for decode your JWT-TOKEN .

(您不需要使用atob()函数来解码JWT-TOKEN 。)

angular have jwt-decode component for this issue.

(角有这个问题的jwt-decode组件。)

you have to install this library:

(您必须安装此库:)

npm install jwt-decode

(npm安装jwt解码)

and import this to your component that you want to decode like:

(并将其导入到要解码的组件中,例如:)

import * as jwt_decode from 'jwt-decode';

(从'jwt-decode'导入*作为jwt_decode;)

Now take your token and decode that.

(现在获取令牌并对其进行解码。)

  getUserProfile() {
    const idToken = localStorage.getItem('id_token');
    return jwt_decode(idToken);
  }

Now you have user profile.

(现在您有了用户个人资料。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...