This repository has been archived by the owner on Feb 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Copy pathjwtArticle.html
276 lines (236 loc) · 12.1 KB
/
jwtArticle.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>Learn Git</title>
<meta content="" name="descriptison">
<meta content="learn, free, learn free, tech, knowledge, learning opportunity, development, coding, programming,website, project" name="keywords">
<!-- Favicons -->
<link href="assets/img/favicon.png" rel="icon">
<link href="assets/img/apple-touch-icon.png" rel="apple-touch-icon">
<!-- Google Fonts -->
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,300i,400,400i,600,600i,700,700i|Roboto:300,300i,400,400i,500,500i,600,600i,700,700i|Poppins:300,300i,400,400i,500,500i,600,600i,700,700i" rel="stylesheet">
<!-- Vendor CSS Files -->
<link href="assets/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">
<link href="assets/vendor/icofont/icofont.min.css" rel="stylesheet">
<link href="assets/vendor/boxicons/css/boxicons.min.css" rel="stylesheet">
<link href="assets/vendor/owl.carousel/assets/owl.carousel.min.css" rel="stylesheet">
<link href="assets/vendor/venobox/venobox.css" rel="stylesheet">
<link href="assets/vendor/aos/aos.css" rel="stylesheet">
<!-- Template Main CSS File -->
<link href="assets/css/style.css" rel="stylesheet">
</head>
<body>
<!-- ======= Header ======= -->
<header id="header" class="fixed-top header-inner-pages">
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-xl-9 d-flex align-items-center">
<h1 class="logo mr-auto"><a href="index.html">Learn Git</a></h1>
<!-- Uncomment below if you prefer to use an image logo -->
<!-- <a href="index.html" class="logo mr-auto"><img src="assets/img/logo.png" alt="" class="img-fluid"></a>-->
<nav class="nav-menu d-none d-lg-block">
<ul>
<li><a href="index.html">Home</a></li>
<!-- <li><a href="#about">About</a></li>
<li><a href="#services">Services</a></li>
<li><a href="#portfolio">Portfolio</a></li>
<li><a href="#team">Team</a></li>
<li class="drop-down"><a href="">Drop Down</a>
<ul>
<li><a href="#">Drop Down 1</a></li>
<li class="drop-down"><a href="#">Deep Drop Down</a>
<ul>
<li><a href="#">Deep Drop Down 1</a></li>
<li><a href="#">Deep Drop Down 2</a></li>
<li><a href="#">Deep Drop Down 3</a></li>
<li><a href="#">Deep Drop Down 4</a></li>
<li><a href="#">Deep Drop Down 5</a></li>
</ul>
</li>
<li><a href="#">Drop Down 2</a></li>
<li><a href="#">Drop Down 3</a></li>
<li><a href="#">Drop Down 4</a></li>
</ul>
</li>
<li><a href="#contact">Contact</a></li>-->
</ul>
</nav><!-- .nav-menu -->
<a href="#about" class="get-started-btn scrollto">Get Started</a>
</div>
</div>
</div>
</header><!-- End Header -->
<main id="main">
<section class="breadcrumbs">
<div class="container">
<div class="d-flex justify-content-between align-items-center">
<h2>Learn Git Page</h2>
<ol>
<li><a href="#">Home</a></li>
<li>Learn Git Page</li>
</ol>
</div>
</div>
</section>
<section class="inner-page">
<div class="container">
<pre>
What is JSON Web Token?
JWT is a stander and secure way to transfer information between parties as a JSON object, that can be verifiable and JWT makes the tokens easy to transfer through an HTTP request.
JWT is mainly used for authentication. After a user signs in to an application, the application then assigns a JWT token to that user. Whenever user will make requests will include the assigned JWT token. This token tells the server what routes, services, and resources the user is allowed to access.
Structure of a JWT
Header — consists of two parts: the type of token (i.e., JWT) and the signing algorithm (i.e., HS512) header.
Payload — Contains the claims that provide information about a user who has been authenticated along with other information such as token expiration time payload
Signature — Final part of a token that wraps in the encoded header and payload, along with the algorithm and a secret.
Implement JWT with Node JS
Before you go ahead with this tutorial, you should have knowledge of express framework Here's the Link
for reference.
Create a file called index.js in the project directory. This is the only file that we create and work on in this project in order to keep things simple. Our main goal is to understand how JWTs work in a Node.js application.
So, let’s go ahead and understand what each snippet of code does in this file.
We are importing a couple of modules that we will be needing in the application: express, and JWT. We can install both modules with this command npm i express jsonwebtoken --save from the command line. Then in the index.js file paste the below code 👇.
const express = require("express");
const jwt = require("jsonwebtoken");
const app = express();
app.use(express.json())
Now we are going to write a post method with the express, which will help us to create a token.
app.post("/api/login", (req, res, next) => {
const user = {
id: 1,
username: "john",
email: "[email protected]"
};
const password = '*******'
if(user.username === req.body.username && password === req.body.password) {
jwt.sign({ user: user }, "secretkey", (err, token) => {
res.cookie('token',token);
res.status(200).json(
"Auth successful"
);
});
}
else {
res.status(401).json('Unauthorized client')
}
});
// server running
app.listen(process.env.PORT || 3000, () => {
console.log("Server started with express..");
});
! Don’t forget to run the index.js file use this command node index.js
Explanation
sign() method to create a JSON Web Token for that user and returns the token in the form of a JSON string. Now you can make a request through the postman on this URL http://localhost:8000/api/login and the token will save in the cookies
To make sure you got token, you can check in the postman, open postman, and in the top-right side you can click cookies, and then you should get token 👇
you can expand it by click on the token
Finally, we define another post method with verifyToken() that takes care of the token verification process. Let’s see How we can implement
app.post("/api/post/verify", (req, res) => {
let token = req.headers.cookie.split("=")[1]
jwt.verify(token, "secretkey", (err, authData) => {
if (err) {
res.sendStatus(403);
} else {
res.json({
message: "verify successfully.",
authData
});
}
});
});
http://localhost:8000/api/post/verify make a request on it
Explanation
We took the token from our req.cookies.split("=")[1] or simplest way to get token use this NPM cookie-parser and then store it in token variable and by the use of .verify() method we are verifying token which we assigned in the previous request
and if the token is valid, will get a message verify successfully on the postman or 403.
HAPPY CODE LIFE
Aadil Raza
</pre>
</div>
</section>
</main><!-- End #main -->
<!-- ======= Footer ======= -->
<footer id="footer">
<!-- <div class="footer-top">
<div class="container">
<div class="row">
<div class="col-lg-3 col-md-6 footer-contact">
<h3>Learn Git</h3>
<p>
A108 Adam Street <br>
New York, NY 535022<br>
United States <br><br>
<strong>Phone:</strong> +1 5589 55488 55<br>
<strong>Email:</strong> [email protected]<br>
</p>
</div>
<div class="col-lg-2 col-md-6 footer-links">
<h4>Useful Links</h4>
<ul>
<li><i class="bx bx-chevron-right"></i> <a href="#">Home</a></li>
<li><i class="bx bx-chevron-right"></i> <a href="#">About us</a></li>
<li><i class="bx bx-chevron-right"></i> <a href="#">Services</a></li>
<li><i class="bx bx-chevron-right"></i> <a href="#">Terms of service</a></li>
<li><i class="bx bx-chevron-right"></i> <a href="#">Privacy policy</a></li>
</ul>
</div>
<div class="col-lg-3 col-md-6 footer-links">
<h4>Our Services</h4>
<ul>
<li><i class="bx bx-chevron-right"></i> <a href="#">Web Design</a></li>
<li><i class="bx bx-chevron-right"></i> <a href="#">Web Development</a></li>
<li><i class="bx bx-chevron-right"></i> <a href="#">Product Management</a></li>
<li><i class="bx bx-chevron-right"></i> <a href="#">Marketing</a></li>
<li><i class="bx bx-chevron-right"></i> <a href="#">Graphic Design</a></li>
</ul>
</div>
<div class="col-lg-4 col-md-6 footer-newsletter">
<h4>Join Our Newsletter</h4>
<p>Tamen quem nulla quae legam multos aute sint culpa legam noster magna</p>
<form action="" method="post">
<input type="email" name="email"><input type="submit" value="Subscribe">
</form>
</div>
</div>
</div>
</div>
-->
<div class="container">
<div class="copyright-wrap d-md-flex py-4">
<div class="mr-md-auto text-center text-md-left">
<div class="copyright">
© Copyright <strong><span>Learn Free</span></strong>. All Rights Reserved
</div>
<div class="credits">
<!-- All the links in the footer should remain intact. -->
<!-- You can delete the links only if you purchased the pro version. -->
<!-- Licensing information: https://bootstrapmade.com/license/ -->
<!-- Purchase the pro version with working PHP/AJAX contact form: https://bootstrapmade.com/techie-free-skin-bootstrap-3/ -->
Designed by <a href="https://bootstrapmade.com/">BootstrapMade</a>
</div>
</div>
<div class="social-links text-center text-md-right pt-3 pt-md-0">
<a href="#" class="twitter"><i class="bx bxl-twitter"></i></a>
<a href="#" class="facebook"><i class="bx bxl-facebook"></i></a>
<a href="#" class="instagram"><i class="bx bxl-instagram"></i></a>
<a href="#" class="google-plus"><i class="bx bxl-skype"></i></a>
<a href="#" class="linkedin"><i class="bx bxl-linkedin"></i></a>
</div>
</div>
</div>
</footer><!-- End Footer -->
<a href="#" class="back-to-top"><i class="icofont-simple-up"></i></a>
<div id="preloader"></div>
<!-- Vendor JS Files -->
<script src="assets/vendor/jquery/jquery.min.js"></script>
<script src="assets/vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<script src="assets/vendor/jquery.easing/jquery.easing.min.js"></script>
<script src="assets/vendor/php-email-form/validate.js"></script>
<script src="assets/vendor/waypoints/jquery.waypoints.min.js"></script>
<script src="assets/vendor/counterup/counterup.min.js"></script>
<script src="assets/vendor/owl.carousel/owl.carousel.min.js"></script>
<script src="assets/vendor/isotope-layout/isotope.pkgd.min.js"></script>
<script src="assets/vendor/venobox/venobox.min.js"></script>
<script src="assets/vendor/aos/aos.js"></script>
<!-- Template Main JS File -->
<script src="assets/js/main.js"></script>
</body>
</html>