nodeauthentication
Dependencies are the crucial packages required for your application to function in a production environment. At the same time,
devDependencies are tools and utilities that aid in the development process but are not needed in production.
npm i -D nodemon[dev dpendency]
userController.js:-
before jwt authentication:-
import UserModel from "../Models/User.js";
import bcrypt from 'bcrypt'
export default class userController{
static userRegistration=async(req,res)=>{
const {username,userpass}=req.body;
// console.log(req.body);
try{
const user=await UserModel.find({username:username})
if(user){
res.status(409).send({status:"failed",message:"username already exists"})
}
else{
const salt = bcrypt.genSaltSync(12);
const hashpassword = bcrypt.hashSync(userpass, salt);
const doc=new UserModel({
username:username,
userpass:hashpassword
})
await doc.save()
res.status(201).send({status:'success',message:"data registerd successfully"});
}
}
catch(error){
console.log(error);
}
}
static userLogin=async(req,res)=>{
try{
const {username,userpass}=req.body;
if(username &&userpass){
const user=await UserModel.findOne({username:username});
if(user!=null){
const ismatchpass=await bcrypt.compare(userpass,user.userpass);
//encrypt password and check with the request
if(user.username===username &&ismatchpass){
res.status(200).send({status:"ture","message":"user successfully login"})
}
else{
res.status(401).send({status:"failed","message":"username or password not match"})
}
}
else{
res.status(401).send({status:"failed","message":"not a registerd user"})
}
}
else{
res.status(499).send({status:"failed","message":"All fields are required"})
}
}
catch(error){
console.log(error);
}
}
}
...
jwt token create while registreation and login:
import UserModel from "../Models/User.js";
import bcrypt from 'bcrypt';
import jwt from 'jsonwebtoken'
export default class userController{
static userRegistration=async(req,res)=>{
const {username,userpass}=req.body;
// console.log(req.body);
try{
const user=await UserModel.find({username:username})
if(user.length!=0){
console.log(user);
res.status(409).send({status:"failed",message:"username already exists"})
}
else{
const salt = bcrypt.genSaltSync(12);
const hashpassword = bcrypt.hashSync(userpass, salt);
const doc=new UserModel({
username:username,
userpass:hashpassword
})
await doc.save()
/*json web token*/
const saved_user=await UserModel.findOne({username:username});//which user we saved
//Generate JWT token
const token=jwt.sign({userID:saved_user._id},process.env.JWT_SECRET_KEY,{expiresIn:'5d'})
res.status(201).send({status:'success',message:"data registerd successfully","token":token});
}
}
catch(error){
console.log(error);
}
}
static userLogin=async(req,res)=>{
try{
const {username,userpass}=req.body;
if(username &&userpass){
const user=await UserModel.findOne({username:username});
if(user!=null){
const ismatchpass=await bcrypt.compare(userpass,user.userpass);
//encrypt password and check with the request
if(user.username===username &&ismatchpass){
/*json web token*/
//Generate JWT token
const token=jwt.sign({userID:user._id},process.env.JWT_SECRET_KEY,{expiresIn:'5d'})
res.status(200).send({status:"ture","message":"user successfully login","token":token})
}
else{
res.status(401).send({status:"failed","message":"username or password not match"})
}
}
else{
res.status(401).send({status:"failed","message":"not a registerd user"})
}
}
else{
res.status(499).send({status:"failed","message":"All fields are required"})
}
}
catch(error){
console.log(error);
}
}
}
...
Comments
Post a Comment