Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

node js passport local for sqlite

var crypto = require('crypto');
var sqlite3 = require('sqlite3');

var db = new sqlite3.Database('./database.sqlite3');

// ...

function hashPassword(password, salt) {
  var hash = crypto.createHash('sha256');
  hash.update(password);
  hash.update(salt);
  return hash.digest('hex');
}

passport.use(new LocalStrategy(function(username, password, done) {
  db.get('SELECT salt FROM users WHERE username = ?', username, function(err, row) {
    if (!row) return done(null, false);
    var hash = hashPassword(password, row.salt);
    db.get('SELECT username, id FROM users WHERE username = ? AND password = ?', username, hash, function(err, row) {
      if (!row) return done(null, false);
      return done(null, row);
    });
  });
}));

passport.serializeUser(function(user, done) {
  return done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  db.get('SELECT id, username FROM users WHERE id = ?', id, function(err, row) {
    if (!row) return done(null, false);
    return done(null, row);
  });
});

// ...

app.post('/login', passport.authenticate('local', { successRedirect: '/good-login',
                                                    failureRedirect: '/bad-login' }));
Comment

PREVIOUS NEXT
Code Example
Javascript :: google auto complete not show on modal 
Javascript :: hello-world.js 
Javascript :: wherein knex 
Javascript :: discord.js ban 
Javascript :: animated scroll to anchor without jquery 
Javascript :: facebook graph X-Hub-Signature 
Javascript :: javascript look hack 
Javascript :: how to push object in array in angular from an api 
Javascript :: setInterval issue, if i turn on new tap, that can be slower 
Javascript :: clima 
Javascript :: redefineFunction 
Javascript :: how to make a website send form to you 
Javascript :: javascript iframe listener 
Javascript :: Using Scrip as Web app 
Javascript :: graphql Int cannot represent non-integer value 
Javascript :: global keyboard shortcuts react 
Javascript :: Generar números aleatorios en Javascript entre un mínimo y un máximo 
Javascript :: react usestate vs variable 
Javascript :: add multiple classes javascript 
Javascript :: country name based on browser 
Javascript :: javascript calculate element style 
Javascript :: how to allow the onclick event of a string in javascript 
Javascript :: how to make a discord js bot get its own message id 
Javascript :: javascript array same class elements 
Javascript :: react-hook-form input 
Javascript :: react passport session 
Javascript :: react native elements datepicker 
Javascript :: any-text npm 
Javascript :: javascript cast everything to string 
Javascript :: online code converter javascript to typescript 
ADD CONTENT
Topic
Content
Source link
Name
6+1 =