角度为4的解码有效负载

时间:2018-04-06 22:31:17

标签: angular

我对角度4的JWT很新,所以有人帮助我如何得到这样的东西:

"sub": "1234567890",
 "name": "John Doe",
 "admin": true,
 "iat": 1523038561,
 "exp": 1523042161

而不是:

exp: 1523045040, nbf: 1523041440, …}
exp:1523045040
iat:1523041440
jti:"V5YPH8VBeQlQnF2e"
nbf:1523041440
sub:1
我使用此代码设置我的令牌

 Signin(email: string, password:string , type:string): Observable<boolean> {
    
   // console.log(info);
    var data = JSON.stringify({email: email, password: password, type:type });
    return this.http.post(this.server +"user/signin" , data, this.options).map((response: Response) => {
      // login successful if there's a jwt token in the response
      let token = response.json() && response.json().token;
      
      if (token) {
          // set token property
          this.token = token;

          // store username and jwt token in local storage to keep user logged in between page refreshes
          localStorage.setItem('currentUser', JSON.stringify({ email:email , token: token , type: type }));

          // return true to indicate successful login
          return true;
      } else {
          // return false to indicate failed login
          return false;
      }
  });
    }

如果我不可能如何使用已解码的令牌

来搜索用户名

1 个答案:

答案 0 :(得分:0)

使用jwt的解码方法,(Check the jwt docs):

// get the decoded payload and header
import { JwtHelperService } from '@auth0/angular-jwt';
const helper = new JwtHelperService();

const decodedToken = helper.decodeToken(myRawToken);