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 …


This content originally appeared on DEV Community and was authored by Deepak

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")
});

we ensure that you can save your secret key in your config file.


This content originally appeared on DEV Community and was authored by Deepak


Print Share Comment Cite Upload Translate Updates
APA

Deepak | Sciencx (2021-12-18T13:14:55+00:00) Secure our website using JWT (JSON Web Token) in nodeJS or expressJS. Retrieved from https://www.scien.cx/2021/12/18/secure-our-website-using-jwt-json-web-token-in-nodejs-or-expressjs/

MLA
" » Secure our website using JWT (JSON Web Token) in nodeJS or expressJS." Deepak | Sciencx - Saturday December 18, 2021, https://www.scien.cx/2021/12/18/secure-our-website-using-jwt-json-web-token-in-nodejs-or-expressjs/
HARVARD
Deepak | Sciencx Saturday December 18, 2021 » Secure our website using JWT (JSON Web Token) in nodeJS or expressJS., viewed ,<https://www.scien.cx/2021/12/18/secure-our-website-using-jwt-json-web-token-in-nodejs-or-expressjs/>
VANCOUVER
Deepak | Sciencx - » Secure our website using JWT (JSON Web Token) in nodeJS or expressJS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/18/secure-our-website-using-jwt-json-web-token-in-nodejs-or-expressjs/
CHICAGO
" » Secure our website using JWT (JSON Web Token) in nodeJS or expressJS." Deepak | Sciencx - Accessed . https://www.scien.cx/2021/12/18/secure-our-website-using-jwt-json-web-token-in-nodejs-or-expressjs/
IEEE
" » Secure our website using JWT (JSON Web Token) in nodeJS or expressJS." Deepak | Sciencx [Online]. Available: https://www.scien.cx/2021/12/18/secure-our-website-using-jwt-json-web-token-in-nodejs-or-expressjs/. [Accessed: ]
rf:citation
» Secure our website using JWT (JSON Web Token) in nodeJS or expressJS | Deepak | Sciencx | https://www.scien.cx/2021/12/18/secure-our-website-using-jwt-json-web-token-in-nodejs-or-expressjs/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.