const express = require("express");
const bodyParser = require("body-parser");
const flash = require("connect-flash");
const expressSession = require("express-session");
const MySQLStore = require("express-mysql-session")(expressSession);
const connection = require("./db/connection");
const helpers = require("./helpers");
const config = require("./config.json");
const moment = require("moment");
const passport = require("passport");
const LocalStrategy = require("passport-local").Strategy;
const crypto = require("crypto");
const cookieParser = require("cookie-parser");
moment.locale("ru");
const app = express();
let listener = require("http").Server(app);
passport.serializeUser(function (user, done) {
done(null, user.id);
});
passport.deserializeUser(function (id, done) {
connection.query("select * from users where id = " + id, (err, rows) => {
done(err, rows[0]);
});
});
passport.use(
"local-signup",
new LocalStrategy(
{
usernameField: "email",
passwordField: "password",
passReqToCallback: true,
},
(req, email, password, done) => {
connection.query(
"select * from users where email = ?",
[email],
(err, rows) => {
if (err) {
return done(err);
}
if (rows.length) {
return done(
null,
false,
req.flash("signupMessage", "That email is already taken.")
);
} else {
// create the user
crypto.pbkdf2(
password,
email,
25000,
512,
"sha256",
(err, hash) => {
let newUserMysql = new Object();
newUserMysql.email = email;
newUserMysql.password = hash.toString("hex");
connection.query(
"INSERT INTO users (email, password) values (?, ?)",
[newUserMysql.email, newUserMysql.password],
(err, rows) => {
newUserMysql.id = rows.insertId;
return done(null, newUserMysql);
}
);
}
);
}
}
);
}
)
);
passport.use(
"local-login",
new LocalStrategy(
{
usernameField: "email",
passwordField: "password",
passReqToCallback: true,
},
(req, email, password, done) => {
connection.query(
"SELECT * FROM `users` WHERE `email` = ?",
[email],
(err, rows) => {
if (err) {
return done(err);
}
if (!rows.length) {
return done(
null,
false,
req.flash("loginMessage", "No user found.")
);
}
crypto.pbkdf2(password, email, 25000, 512, "sha256", (err, hash) => {
password = hash.toString("hex");
if (!(rows[0].password == password)) {
return done(
null,
false,
req.flash("loginMessage", "Oops! Wrong password.")
);
}
});
return done(null, rows[0]);
}
);
}
)
);
const routes = require("./routes.js")(express.Router(), passport);
app
.use(
express.static("static", {
maxage: "4h",
})
)
.use(cookieParser())
/*.use(i18n.init)*/
.set("view engine", "ejs")
.use(bodyParser.json())
.use(
bodyParser.urlencoded({
extended: true,
})
)
.use(
expressSession({
secret: config.express.secret,
store: new MySQLStore({}, connection),
resave: false,
saveUninitialized: false,
})
)
.use(passport.initialize())
.use(flash())
.use(passport.session())
.use(async (req, res, next) => {
res.locals.error = null;
res.locals.helpers = helpers;
res.locals.user = null;
res.locals.moment = moment;
res.locals.url = req.url;
next();
})
.use(routes)
.use((req, res, next) => {
let err = new Error("Здесь ничего нет");
err.status = 404;
next(err);
})
.use((err, req, res, next) => {
if (err.status != 404) {
err.message = "Неизвестная ошибка";
}
return res.status(err.status || 500).render("error", {
message: err.message,
error: req.app.get("env") === "development" ? err : null,
status: err.status || 500,
});
});
let server = listener.listen(config.express.port || 3000, () => {
const host =
server.address().address == "::" ? "localhost" : server.address().address;
const port = server.address().port;
console.log("Start at http://%s:%s", host, port);
});
Обновление-2
Картинки. Поиск: .then(), .catch(), or invoked await
Поиск: .then(), .catch() await
Обновление-2
Поиск .then(), .catch() await
Обновление-2
\db\connection.js
const mysql = require('mysql2');
const config = require('../config.json');
const connection = mysql.createConnection({
host: config.mysql.host,
user: config.mysql.user,
password: config.mysql.password,
database: config.mysql.db,
charset: 'utf8_general_ci',
multipleStatements: true
});
connection.connect();
module.exports = connection;
Картинка. Поиск "mysql2"