10 examples of 'jwt decode npm' in JavaScript

Every line of 'jwt decode npm' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
1export default function decodeJWT (token) {
2 const base64Url = token.split('.')[1];
3 const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
4 const buff = window.nodeAPI.Buffer.from(base64, 'base64');
5 const ascii = buff.toString('ascii');
6 const jsonPayload = decodeURIComponent(ascii.split('').map(function(c) {
7 return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
8 }).join(''));
9
10 return JSON.parse(jsonPayload);
11}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

41function decode(token) {
42 try {
43 const decoded = jwt.verify(token, secretKey);
44 if (decoded.iss != ISSUER) {
45 throw new ccxtRestErrors.AuthError(`Invalid issuer ${decoded.iss}`)
46 }
47 return decoded
48 } catch (error) {
49 console.trace(error)
50 throw new ccxtRestErrors.InvalidTokenError(`Invalid token ${token}`)
51 }
52}
41function decodeJwt(token) {
42 const parts = token.split('.');
43
44 if (parts.length !== 3) {
45 throw new Error('JWT must have 3 parts');
46 }
47
48 let payload = parts[1].replace(/-/g, '+').replace(/_/g, '/');
49 switch (payload.length % 4) {
50 case 0: { break; }
51 case 2: { payload += '=='; break; }
52 case 3: { payload += '='; break; }
53 default: {
54 throw 'Illegal base64url string!';
55 }
56 }
57
58 const decodedPayload = $window.decodeURIComponent($window.escape($window.atob(payload)));
59 if (!decodedPayload) {
60 throw new Error('Cannot decode the token');
61 }
62
63 return JSON.parse(decodedPayload);
64}
35function parseJwt(token: any): string {
36 var base64Url = token.split('.')[1];
37 var base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
38 return JSON.parse(window.atob(base64));
39};
15export function decodeJWT(str: string) {
16 const parts = str.split('.');
17 if (parts.length < 2) {
18 return null;
19 }
20 return {
21 header: decodeBase64Url(parts[0]),
22 payload: decodeBase64Url(parts[1]),
23 signature: parts[2]
24 };
25}
8export function decode(token) {
9 let parts = token.split('.')
10 let [header, claims, signature] = parts.map(b64d);
11 return {
12 header: JSON.parse(header),
13 claims: JSON.parse(claims),
14 signature,
15 token,
16 parts
17 }
18}
10function parseJwt(rawJwt) {
11 try {
12 return jwt.verify(rawJwt, process.env.JWT_SECRET)
13 } catch (e) {
14 console.log(e)
15 return null
16 }
17}
46decode(token) {
47 if (!this.isValid(token)) {
48 return {}
49 }
50
51 try {
52 return jwtDecode(token);
53 } catch (err) {
54 return {}
55 }
56}
119public decodeToken(token: string): any {
120
121 const parts = token.split('.');
122
123 if (parts.length !== 3) {
124 throw new Error('JWT must have 3 parts');
125 }
126
127 const decoded = this.urlBase64Decode(parts[1]);
128 if (!decoded) {
129 throw new Error('Cannot decode the token');
130 }
131
132 return JSON.parse(decoded);
133}
25function decode (token) {
26 if (typeof token !== 'string' || token[0] !== '~' || token.length < 88) {
27 return { ok: false, err: new Error('Mailformed token') }
28 }
29 const payload = token.slice(87)
30 return {
31 ok: true,
32 payload,
33 data: deserialize(payload),
34 signature: Buffer.from(token.slice(1, 87), 'base64')
35 }
36}

Related snippets