

Secure our website using JWT (JSON Web Token) in nodeJS or expressJS
source link: https://dev.to/deepakjaiswal/secure-our-website-using-jwt-json-web-token-in-nodejs-or-expressjs-5a7d
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.


Posted on Dec 18
Secure our website using JWT (JSON Web Token) in nodeJS or expressJS
here we are using JWT to secure our application or website from unauthenticated user they try to access our data.
In npmjs a library named is
jsonwebtoken
npm i jsonwebtoken
if we only check user isAuthenticated or not we simply pass the middleware in between request and response
auth.js
export default function getTokenFromUser(req: Request) {
const authorization = req.headers.token;
var decoded = jwt.verify(authorization, 'secret');
if (!decoded) {
throw new TokenError("No Authorization Header");
}
try {
const token = decoded?.split("User data ")[1];
return token;
} catch {
throw new TokenError("Invalid Token Format");
}
}
we simple pass this auth of in between req,res
app.post('/api/post',auth,(req,res)=>{
//if some operation on code we use middleware
const token=jwt.sign({
data: 'your data to store as token'
}, 'secret', { expiresIn: '1h' });
res.header('token',token).send("success")
});
Enter fullscreen mode
Exit fullscreen mode
we ensure that you can save your secret key in your config file.
Recommend
-
66
Readme.md
-
32
UPDATE: As of 19/08/2019 _api will not be actively maintained, I'm working on a WebSocket based implementation, I may in the future rewrite this but I just don't use HTTP Requests/Responses in my work/personal projects any more. T...
-
10
AngularJS tutorial for beginners with NodeJS ExpressJS and MongoDB (Part I)
-
8
-
8
JSON Web Token (JWT) Implementation Using Node.js JWTs offer a way to manage security concerns. In this post, learn how to generate a JWT in four...
-
5
Read Excel File in JSON Format Using Nodejs This tutorial help to read excel file using nodejs. We’ll let you know to read the excel file in JSON format and insert it into MySQL data or NoSQL database using rest API.
-
7
Intro In this tutorial we will create a simple API with NodeJs and ExpressJS, connect to MongoDB and perform CRUD operations with the API. Step by Step Process ...
-
18
JSON Web Token Verification in Ktor using Kotlin and Java-JWT Scott Brady 20 November 2017 ・ Kotlin ・ Updated Fe...
-
9
Introduction We create accounts and login into our accounts on various websites. The process of proving the identity of a user can be called Authentication. It is one of the essential parts of any application to provide a b...
-
6
What is a CSRF token? A CSRF token is a unique value that is generated by the server and stored on the client’s session. This token is included in every subsequent request made by the client, either in an HTT...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK