Posts

Showing posts from November, 2022

Registration and login authentication in express js

UserController.js import userModel from "../models/User.js" ; import bcrypt from 'bcrypt' class userController {   static home = ( req , res ) => {     res . render ( "index" );   };   static registration = ( req , res ) => {     res . render ( "registration" );   };   static createUserDoc = async ( req , res ) => {     const hashPassword = await bcrypt . hash ( req . body . password , 10 )     console . log ( req . body )     try {       const doc = new userModel ({         name : req . body . name ,         email : req . body . email ,         password : hashPassword ,       });       await doc . save ();       res . redirect ( "/login" );     } catch ( error ) {       console . log ( error );     }   };   stat...

Session with mongodb storage in express js

 1. app.js import express from 'express' import web from './routes/web.js' import session from 'express-session' import connectDb from './db/connectdb.js' import MongoStore from 'connect-mongo' const app = express () const port = process . env . PORT || '3000' const Database_Url = process . env . Database_Url || "mongodb://localhost:27017" connectDb ( Database_Url ) //Mongo DB Session. const sessionStore = MongoStore . create ({     mongoUrl : Database_Url ,     dbName : "school22" ,     collectionName : 'sessions' ,     ttl : 14 * 24 * 60 * 60 , //session time     autoNative : 'native' }) //session app . use ( session ({     name : 'SessionGreek' ,     secret : 'Iamkey' ,     resave : false ,     saveUninitialized : true ,     cookie : { maxAge : 20000 },     store : sessionStore })) //load routes app . use ( '/' , web ) app . listen ( port ,() =...

Session in express.

 express express-session