Skip to main content

Posts

Showing posts from 2022

How to Register User with Hashed Password node js & express js

 // Create a a file named helper.js to convert plain password into hashed password as well compare it at the time of login - //helper.js import bcrypt from 'bcryptjs';  function hashPassword(password){ const salt = bcrypt.genSaltSync();  return bcrypt.hashSync(password, salt);  } function comparePassword(raw, hash){ return bcrypt.compareSync(raw, hash) }    // here above in compare password function first argument is raw pass means plain pass which you have entered and second hash is that already saved hashed password , we will compare it and get user login export {hashPassword, comparePassword }  // End Helper ====================================================== // now at your IndexModel or User Model  import that functions which were created in helper.js as well RegisterSchema also  import RegisterSchemaModel from '../schema/RegisterSchema.js'; import {hashPassword, comparePassword} from '../schema/helper.js'; class IndexModel{...

API for Updating User Profile on Mongoose (POSTMAN) Node js, Express js

// change code According to your data   // Create your own Schema & import  import RegisterSchemaModel from '../schema/RegisterSchema.js'; router.post("/api/admin/epadmin",(req, res) => {   //You can pass req.body directly or you can separate object       const uData = {"name":req.body.name,"lname":req.body.lname,"mobile":req.body.mobile,"address":req.body.adddress,"city":req.body.city,"gender":req.body.gender}; // Data to be Updated      const cData = {"email":req.body.email}; // Conditional Data or Unqiue Key to fetch      const updatedUser =  RegisterSchemaModel.findOneAndUpdate(cData, uData,{new:true}).then((result)=>{     return res.send(result)   }).catch(error => {     return res.status(500).send(error);   });   return res.status(200).json({     message : "Updated user",     data: updatedUser   }); });

Send Verification Mail on User Registration Node Js without SSL certificate Solution - self signed certificate in certificate chain nodejs

First do npm install nodemailer   & then import it in backend code    ========== Backend Code === create EmailAPI.js file in your router folder & then import it to your IndexRouter.js file  // EmailAPI.js  import nodemailer from 'nodemailer'; var sendMail=(email,password,token)=>{    process.env.NODE_TLS_REJECT_UNAUTHORIZED='0' var transporter = nodemailer.createTransport({   service: 'gmail',   auth: {     user: 'vasubirla@gmail.com',     pass: 'mtvrqzmxlarnrg9h' // Create your own temporary password from Google setting     } }); var mailOptions = {   from: 'vasubirla@gmail.com',   to: email,   subject: 'Verification Mail MyApp',   html: "<h1>Welcome To MyApp</h1><p>you have successfully register to our app , your login credentials are attached below</p><h3>Email : "+email+"</h3><h3>Password : "+password+"<h3>OTP for account...

Fetch User Profile on UI to Edit - How to Fetch User Details to Edit Profile Node JS, Express JS

How to Get The Profile of Current User in Node JS // Front End code   <h2> Edit Your Profile Here </h2> <font color="blue"> <%- output %></font> <form method="post" action="/admin/epadmin"> <div> <label for="name">First Name:</label> <input type="text" name="name" value="<%- userDetails.name %>"> </div> <br><br> <div> <label for="lname">Last Name:</label> <input type="text" name="lname" value="<%- userDetails.lname %>"> </div> <br><br> <div> <label for="email">Email:</label> <input readonly type="email" name="email" value="<%- userDetails.email %>"> </div> <br><br> <br><br> <div> <label for="m...

APIs for mongoDB data to Update, Insert , Delete, Get Data ( Postman API Testing also )

 const express = require('express'); const app = express(); const { MongoClient, ObjectId } = require('mongodb') // API for get data start const url = "mongodb://localhost:27017/"; app.get('/', (req, res) =>{   MongoClient.connect(url, function(err, db) {     if (err) throw err;     var dbo = db.db("vasu"); // here vasu is database name     dbo.collection("tnl_register").find().toArray(function(err, result) {       if (err) throw err;       console.log(result);       res.send(result);       db.close();     });   }); }); // API for get one record start app.get('/:id', (req, res) =>{   MongoClient.connect(url, function(err, db) {     if (err) throw err;     var dbo = db.db("vasu");     const query = {"_id": ObjectId(req.params.id)};     dbo.collection("tnl_register").findOne(query, (err, result) => {       i...

Query To insert data into MongoDB Database using form data by Postman

 //node es5  var MongoClient = require('mongodb').MongoClient; var url = "mongodb://localhost:27017/"; MongoClient.connect(url, function(err, db) {   if (err) throw err;   var dbo = db.db("mydb");   //Create a collection name "customers":   dbo.createCollection("customers", function(err, res) {     if (err) throw err;     console.log("Collection created!");     db.close();   }); }); //for express ES6 import * as mongodb from 'mongodb'; import { MongoClient } from 'mongodb' const url ="mongodb://localhost:27017/machinetest"; MongoClient.connect(url,(err,db)=>{             //console.log(db);         if(err) throw err ;         var dbo = db.db('vasu');         const query = {name: req.body.name, email:req.body.email, password:req.body.password, mobile:req.body.mobile};            dbo.collection("register"...