"SELECT * FROM users WHERE email='"+email+"'"
passport.use(new LocalStrategy({ usernameField: 'email' },
function(email, password, done) {
connection.query("SELECT * FROM users WHERE email LIKE '"+email+"'", function(err,res){
if (err) {
return done(null,false);
console.log(err);
}
else {
console.log(res);
return done(null,res);
}
});
// if (rows.length) {
// return done(null, false, req.flash('signupMessage', 'That email is already taken.'));
// }
// else {
// // if there is no user with that email
// // create the user
// var newUserMysql = new Object();
// newUserMysql.email = email;
// newUserMysql.password = password; // use the generateHash function in our user model
// var insertQuery = "INSERT INTO users ( email, password ) values ('" + email +"','"+ password +"')";
// console.log(insertQuery);
// connection.query(insertQuery,function(err,rows){
// newUserMysql.id = rows.insertId;
// return done(null, newUserMysql);
// });
// }
}
))
const express = require('express');
const session = require('express-session');
const FileStore = require('session-file-store')(session);
const passport = require('passport');
const app = express();
const port = 3000;
app.use(express.json());
app.use(express.urlencoded({extended: false}));
app.use(
session({
secret: "secret",
store: new FileStore(),
cookie: {
path: "/",
httpOnly: true,
maxAge: 60 * 60 * 1000
},
resave: false,
saveUnitialized: false
})
);
require('./config');
app.use(passport.initialize());
app.use(passport.session());
const logout = (req,res,next) => {
if(req.isAuthenticated()) {
return res.redirect('/admin');
} else {
next()
}
}
app.get('/',logout, (req, res) => res.sendFile(__dirname + '/index.html'));
app.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user) {
if (err) {
return next(err);
}
if (!user) {
return res.redirect('/');
console.log(user);
}
req.logIn(user, function(err) {
if (err) {
return next(err);
}
return res.redirect('/admin');
});
})(req, res, next);
});
app.post('/register', function(req,res) {
User.create({ email: "Jane", password: "Doe" }).then(jane => {
console.log("Jane's auto-generated ID:", jane.id);
});
});
const auth = (req,res,next) => {
if(req.isAuthenticated()) {
next()
} else {
return res.redirect('/');
}
}
app.get('/admin', auth, (req, res) => {
res.send('Admin page');
});
app.get('/logout', (req,res) => {
req.logout();
res.redirect('/');
});
app.listen(port, () => console.log(`Example app listening on port ${port}!`));
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const Sequelize = require('sequelize');
// Option 1: Passing parameters separately
const sequelize = new Sequelize('test', 'root', '', {
host: 'localhost',
dialect: 'mysql',
operatorsAliases: false,
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
}
// dialectOptions: {
// socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock',
// supportBigNumbers: true,
// bigNumberStrings: true
// },
});
sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
const User = sequelize.define('users', {
// attributes
email: {
type: Sequelize.STRING,
allowNull: false
},
password: {
type: Sequelize.STRING
// allowNull defaults to true
}
}, {
// options
});
let userDB = null;
sequelize
.query('SELECT * FROM users', { raw: true })
.then(users => {
const isIds = users[0].map(user => user.id);
const isEmails = users[0].map(user => user.email);
const isPasswords = users[0].map(user => user.password);
userDB = {
id: isIds,
email: isEmails,
password: isPasswords
}
});
for(i=0; i<userDB.id.length; i++){
passport.serializeUser(function(user, done) {
console.log("Serialize: ", user.email[i]);
done(null, user.id[i]);
});
passport.deserializeUser(function(id, done) {
console.log("Deserialize: ", id);
user = (userDB.id[i] === id) ? userDB : false;
done(null, user);
});
passport.use(new LocalStrategy({ usernameField: 'email' },
function(email, password, done) {
console.log(email + '/' + password)
console.log(userDB.email[i].split(' ') + '/' + userDB.password[i].split(' '))
if(password == userDB.password[i]) {
console.log(userDB.email[i])
return done(null,userDB)
}
else {
console.log(userDB.password[i])
return done(null,false)
}
})
);
}