Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Bare Minimum Passport Login Pages ExpressJS Only (Routes/Controller Page)

var express = require('express');
var router = express.Router(); 
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const User = require('../models/user'); 
const bcrypt = require('bcrypt');
const saltRounds = 10;

passport.use('local',new LocalStrategy(
  function(username, password, done) {
    User.findOne({ username: username }, function (err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false); }
      if (!bcrypt.compare(user.password, password)) { return done(null, false); }
      return done(null, user);
    });
  }
));
router.get("/", async  function(req, res, next) {
  res.render('index', {title: "Express"});
});
 
router.get("/main", async  function(req, res, next) {
	res.json({success: "success"});
});

router.get("/failure", async  function(req, res, next) {
	res.json({failure: "failure"});
});

router.post("/login", async function(req, res, next){
  passport.authenticate('local', {
    successRedirect: '/main',
    failureRedirect: '/failure',
    failureFlash: true
  })(req, res, next);
});
module.exports = router;
Comment

Bare Minimum Passport Login Pages App.js

/*....*/
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

const passport = require('passport');
var flash    = require('connect-flash');
var session      = require('express-session');

/*mongodb*/

// Set up mongoose connection
var mongoose = require('mongoose');
/*mongodb stuff*/ 
 
//////
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(session({ secret: 'unknown' })); // session secret

// Passport middleware

app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
 
app.use('/', indexRouter);
app.use('/users', usersRouter);

Comment

PREVIOUS NEXT
Code Example
Javascript :: inspect vuex store 
Javascript :: jquery attrib 
Javascript :: generate package json for existing project 
Javascript :: Simple Cryptocurrency Blockchain Using JavaScript 
Javascript :: Vue Js The specified value cannot be parsed, or is out of range 
Javascript :: how to add random color in chart in react j 
Javascript :: Object methods + Static methods javascript 
Javascript :: datatables data in one line 
Javascript :: sol.common.MapTable elo 
Javascript :: show hide element with javascript stack overflow 
Javascript :: ...args javascript 
Javascript :: js how to get random number (inclusive min max) and push it in an array 
Javascript :: JavaScript Using es6(ES2015) Destructuring assignment 
Javascript :: 1--Reverse Bits Algo 
Javascript :: var logNums = function(num) {}; 
Javascript :: react native webview get query params 
Javascript :: PostManDocs 
Javascript :: How to Loop Through an Array with a forEach Loop in JavaScript 
Javascript :: Backbone Model Fetch 
Javascript :: send form data to endpoint js 
Javascript :: discord.js profile picture 
Javascript :: add seconds to date 
Javascript :: jquery equivalent of number_format( 
Javascript :: button click event 
Javascript :: moment duration 
Javascript :: emoji picker react 
Javascript :: javascript return multiple values 
Javascript :: how to convert roman to decimal in javascript 
Javascript :: Accessing HTML attributes in DOM 
Javascript :: JavaScript Changing Prototype 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =