diff --git a/app.js b/app.js index d816b63..069a593 100644 --- a/app.js +++ b/app.js @@ -31,7 +31,8 @@ app.use("/register", registerRoutes); app.get("/", middleware.requireLogin, (req, res, next) => { const payload = { - pageTitle: "Home" + pageTitle: "Home", + userLoggedIn: req.session.user } res.status(200).render("home.pug", payload); diff --git a/routes/loginRoutes.js b/routes/loginRoutes.js index a1607fa..6daab58 100644 --- a/routes/loginRoutes.js +++ b/routes/loginRoutes.js @@ -1,4 +1,7 @@ const express = require('express'); +const bcrypt = require('bcrypt'); +const bodyParser = require('body-parser'); +const User = require('../schemas/UserSchema'); const app = express(); const router = express.Router(); @@ -6,9 +9,44 @@ const router = express.Router(); app.set("view engine", "pug"); app.set("views", "views"); +app.use(bodyParser.urlencoded({ extended: false})); router.get("/", (req, res, next) => { res.status(200).render("login.pug"); }) +router.post("/", async (req, res, next) => { + + const payload = req.body; + + if (req.body.logUsername && req.body.logPassword) { + const user = await User.findOne({ + $or: [ + {username: req.body.logUsername}, + {email: req.body.logUsername} + ] + }).catch((err) => { + console.log(err); + payload.errorMessage = "Что-то пошло не так..."; + res.status(200).render("login.pug", payload); + }); + + if (user != null) { + const result = await bcrypt.compare(req.body.logPassword, user.password); + + if (result === true) { + req.session.user = user; + return res.redirect("/"); + } + } + + payload.errorMessage = "Некоректные данные"; + res.status(200).render("login.pug", payload); + } + + payload.errorMessage = "Заполните все поля"; + res.status(200).render("login.pug", payload); + +}) + module.exports = router; \ No newline at end of file diff --git a/routes/registerRoutes.js b/routes/registerRoutes.js index 51f8aee..0effc81 100644 --- a/routes/registerRoutes.js +++ b/routes/registerRoutes.js @@ -1,15 +1,15 @@ -const express = require('express') -const bcrypt = require('bcrypt') +const express = require('express'); +const bcrypt = require('bcrypt'); const bodyParser = require('body-parser'); -const User = require('../schemas/UserSchema') +const User = require('../schemas/UserSchema'); -const app = express() +const app = express(); const router = express.Router(); app.set("view engine", "pug"); app.set("views", "views"); -app.use(bodyParser.urlencoded({ extended: false})) +app.use(bodyParser.urlencoded({ extended: false})); router.get("/", (req, res, next) => { diff --git a/views/home.pug b/views/home.pug index a53e16f..675da9d 100644 --- a/views/home.pug +++ b/views/home.pug @@ -2,4 +2,4 @@ extends layouts/main-layout.pug block content h1 This is awesome - p luck \ No newline at end of file + p #{userLoggedIn.firstName} \ No newline at end of file diff --git a/views/login.pug b/views/login.pug index 4de8b02..040de2d 100644 --- a/views/login.pug +++ b/views/login.pug @@ -4,7 +4,10 @@ block content .loginContainer h1 Войти form(method="post") - input(type="text", name="logUsername", placeholder="Введите имя или почту", required="") + + p.errorMessage #{errorMessage} + + input(type="text", name="logUsername", placeholder="Введите имя или почту", value=logUsername, required="") input(type="password", name="logPassword", placeholder="Введите пароль", required="") input(type="submit", value="Войти") a(href="/register") Зарегистрироваться