You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<b><a href="#table-of-contents">↥ back to top</a></b>
1417
-
</div>
1418
-
1419
1407
## Q. How to use JSON Web Token (JWT) for authentication in Node.js?
1420
1408
1421
1409
JSON Web Token (JWT) is an open standard that defines a compact and self-contained way of securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed.
@@ -1425,7 +1413,7 @@ There are some advantages of using JWT for authorization:
1425
1413
* Purely stateless. No additional server or infra required to store session information.
1426
1414
* It can be easily shared among services.
1427
1415
1428
-
JSON Web Tokens consist of three parts separated by dots (.), which are:
@@ -1501,128 +1489,6 @@ The `jwt.sign()` method takes a payload and the secret key defined in `config.js
1501
1489
<b><a href="#table-of-contents">↥ back to top</a></b>
1502
1490
</div>
1503
1491
1504
-
## Q. How to implement asymmetric cryptography when signing and verify JSON Web Token (JWT) for authentication in Node.js?
1505
-
1506
-
JSON Web Token (JWT) is an open standard that defines a compact and self-contained way of securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed.
1507
-
1508
-
There are some advantages of using JWT for authorization:
1509
-
1510
-
- Purely stateless. No additional server or infra required to store session information.
**1. Header** - Consists of two parts: the type of token (i.e., JWT) and the signing algorithm (i.e., HS512)
1520
-
1521
-
**2. Payload** - Contains the claims that provide information about a user who has been authenticated along with other information such as token expiration time.
1522
-
1523
-
**3. Signature** - Final part of a token that wraps in the encoded header and payload, along with the algorithm and a secret
1524
-
1525
-
**Installation:**
1526
-
1527
-
```bash
1528
-
npm install jsonwebtoken bcryptjs --save
1529
-
```
1530
-
1531
-
**Usage:**
1532
-
1533
-
1.`mkdir certs` then run `cd certs`
1534
-
1535
-
**Inside the certs folder generate public and private key pairs:**
/** Use the Access token Public Key to verify the JWT access token */
1604
-
jwt.verify(
1605
-
token,
1606
-
ACCESS_TOKEN_PUB_KEY,
1607
-
{ algorithms: ['RS256'] },
1608
-
(err, user) => {
1609
-
console.log(err);
1610
-
if (err) res.status(403);
1611
-
console.log(user);
1612
-
}
1613
-
);
1614
-
```
1615
-
1616
-
The `jwt.sign()` method takes a payload, private key defined in `./certs/accessTokenPrivateKey.pem` and an object which contains other information about the token, this includes the algorithm `{ algorithm: 'RS256'}`. It creates a unique string of characters representing the payload.
<b><a href="#table-of-contents">↥ back to top</a></b>
1624
-
</div>
1625
-
1626
1492
## Q. How to build a microservices architecture with Node.js?
1627
1493
1628
1494
Microservices are a style of **Service Oriented Architecture (SOA)** where the app is structured on an assembly of interconnected services. With microservices, the application architecture is built with lightweight protocols. The services are finely seeded in the architecture. Microservices disintegrate the app into smaller services and enable improved modularity.
0 commit comments