Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

cookie-session use in node

app.use(
  cookieSession({
    maxAge: 30 * 24 * 60 * 60 * 1000,
    keys: [keys.cookieKey]
  })
);
app.use(passport.initialize());
app.use(passport.session());
Comment

how to use session with cookie js nodejs

// Importing express module
const express = require("express")
  
// Importing express-session module
const session = require("express-session")
  
// Importing file-store module
const filestore = require("session-file-store")(session)
  
const path = require("path")
  
// Setting up the server
var app = express()
  
// Creating session 
app.use(session({
    name: "session-id",
    secret: "GFGEnter", // Secret key,
    saveUninitialized: false,
    resave: false,
    store: new filestore()
}))
  
// Asking for the authorization
function auth(req, res, next) {
    // Checking for the session
    console.log(req.session)
  
    // Checking for the authorization
    if (!req.session.user) {
        var authHeader = req.headers.authorization;
        console.log(authHeader);
        var err = new Error("You are not authenticated")
        res.setHeader("WWW-Authenticate", "Basic")
        err.status = 401
        next(err)
  
        var auth = new Buffer.from(authHeader.split(' ')[1],
            "base64").toString().split(":")
  
        // Reading username and password
        var username = auth[0]
        var password = auth[1]
        if (username == "admin2" && password == "password") {
            req.session.user = "admin2"
            next()
        }
        else {
            // Retry incase of incorrect credentials
            var err = new Error('You are not authenticated!');
            res.setHeader("WWW-Authenticate", "Basic")
            err.status = 401;
            return next(err);
        }
    }
    else {
        if (req.session.user === "admin2") {
            next()
        }
        else {
            var err = new Error('You are not authenticated!');
            res.setHeader("WWW-Authenticate", "Basic")
            err.status = 401;
            return next(err);
        }
    }
}
  
// Middlewares
app.use(auth)
app.use(express.static(path.join(__dirname, 'public')));
  
// Server setup
app.listen(3000, () => {
    console.log("Server is Starting")
})
Comment

how to create session cookie in node js

npm install express cookie-parser
Comment

PREVIOUS NEXT
Code Example
Javascript :: row no datatable 
Javascript :: sort an array in descending order javascript 
Javascript :: oop js 
Javascript :: javascript array every 
Javascript :: angularfire 
Javascript :: double exclamation mark javascript 
Javascript :: deep copy array of objects javascript 
Javascript :: Fetch data from multiple pages of an API in react native 
Javascript :: react native extract cookie from response 
Javascript :: decode jwt token online 
Javascript :: grouping related html form input 
Javascript :: set timeout with no name 
Javascript :: tempusdominus calendar out of background size 
Javascript :: how to use handlebars.registerhelper if null 
Javascript :: svelte function at interval 
Javascript :: scriptcase transforming local to global variable 
Javascript :: ajax each 
Javascript :: Laravel summernote HTML output 
Javascript :: javascript array includes time complexity 
Javascript :: javascript vuelidate identical passwords only if checkbox is ticked 
Javascript :: Display name instead ID modal dropdown in angularjs 
Javascript :: angularjs getting Error: [$rootScope:inprog] $digest already in progress when changed from Fetch to $http + $q 
Javascript :: Angular js set default tab with ng-repeat in array object 
Javascript :: Context: Cannot read properties of undefined 
Javascript :: on veiwport reveal javascript 
Javascript :: How to access a preexisting collection with Mongoose 
Javascript :: morgan 
Javascript :: clickable image full screen javascript 
Javascript :: short-circuit evaluation , use of || operator 
Javascript :: Declare Function In Class Constructor 
ADD CONTENT
Topic
Content
Source link
Name
8+5 =