Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

created logging in #2

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
38 changes: 38 additions & 0 deletions routes/loginRoutes.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,52 @@
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();

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;
10 changes: 5 additions & 5 deletions routes/registerRoutes.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion views/home.pug
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ extends layouts/main-layout.pug

block content
h1 This is awesome
p luck
p #{userLoggedIn.firstName}
5 changes: 4 additions & 1 deletion views/login.pug
Original file line number Diff line number Diff line change
Expand Up @@ -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") Зарегистрироваться