Pages
- Export tabe Excel in Asp.net
- CSS
- Date Picker in Jquery
- SQL
- Sort Self Code
- JavaScript(Print Page)
- Web Services Part-4(How to make changes to wcf ser...
- WCF Services Part-3( WCF service implementing mult...
- Home
- WCF Services Part-2(configuration web.config)
- WCF Services Part-1
- Web Services part-1 (introduction)
- Web Service PART-5 ( Call Web Service using JQuery...
- Web Service PART-4 ( Calling asp net web service f...
- Web Service PART-3 (WebMethod attribute properties...
- Web Service PART-2 (Session State )
Tuesday, 5 January 2021
Create and verify jwt tocken in nodeJs
========================Create
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
exports.login = async (req, res, next) => {
const email = req.query.email;
const password = req.query.password;
let loadedUser;
try {
var user = await User.findOne({ email: email });
if (!user) {
res.status(200).json({ Message: 'A user with this email(' + email + ') could not be found.', Result: false });
}
loadedUser = user;
var isEqual = await bcrypt.compare(password, user.password);
if (!isEqual) {
const error = new Error('Wrong password!');
error.statusCode = 200;
res.status(200).json({ Message: "'Wrong password!'", Result: false });
throw error;
};
const token = jwt.sign(
{
email: loadedUser.email,
userId: loadedUser._id.toString()
},
'somesupersecretsecret',
{ expiresIn: '1h' }
);
res.status(200).json({
token: token, userId: loadedUser._id.toString(), Result: true,
cdata: {
email: loadedUser.email,
password: loadedUser.password,
_id: loadedUser._id,
name: loadedUser.name
}
});
} catch (error) {
res.status(201).json({ Message: error.message, response: error, Result: false });
}
};
===================== Verify
const jwt = require('jsonwebtoken');
module.exports = (req, res, next) => {
try {
const authHeader = req.get('Authorization');
if (!authHeader) {
const error = new Error('Not authenticated.');
error.statusCode = 401;
throw error;
}
const token = authHeader.split(' ')[1];
let decodedToken;
try {
decodedToken = jwt.verify(token, 'somesupersecretsecret');
} catch (err) {
err.statusCode = 500;
throw err;
}
if (!decodedToken) {
const error = new Error('Not authenticated.');
error.statusCode = 401;
throw error;
}
req.userId = decodedToken.userId;
next();
} catch (error) {
res.status(500).json({ Message: error.message, response: error, Result: false });
}
};
Subscribe to:
Post Comments (Atom)
IIS deployment support details
Node JS - IIS deployment support details node: http://go.microsoft.com/?linkid=9784334 IISNode: https://github.com/azure/iisnode/releases/...
Pages
- Home
- Export tabe Excel in Asp.net
- CSS
- Sort Self Code
- SQL
- JavaScript(Print Page)
- Date Picker in Jquery
- WCF Services Part-3( WCF service implementing mult...
- Web Services Part-4(How to make changes to wcf ser...
- WCF Services Part-2(configuration web.config)
- WCF Services Part-1
- Web Services part-1 (introduction)
- Web Service PART-5 ( Call Web Service using JQuery...
- Web Service PART-4 ( Calling asp net web service f...
- Web Service PART-3 (WebMethod attribute properties...
- Web Service PART-2 (Session State )
-
@ NgModule ({ declarations: [ GrdFilterPipe ] }) import { Pipe , PipeTransform } from '@angular/core' ; @ Pipe...
-
======Angular onFileChange(event: any) { var reader: any = new FileReader(); if (event.target.files && event.target.files.l...
-
public void MailSant( string Subject, string message_text, string to1) { //------------ Mail Code Start here .....
No comments:
Post a Comment