Skip to content

Commit

Permalink
login feature
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelotrajano committed Jul 11, 2020
1 parent 2931ac3 commit 575c765
Show file tree
Hide file tree
Showing 27 changed files with 504 additions and 143 deletions.
11 changes: 11 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const {
allowInsecurePrototypeAccess,
} = require("@handlebars/allow-prototype-access");
const path = require(`path`);
const passport = require(`passport`);
require(`./config/auth`)(passport);
const session = require("express-session");
const flash = require("connect-flash");
const SERVER_PORT = process.env.PORT || 8787;
Expand All @@ -15,10 +17,13 @@ const feature = require(`./routes/feature`);
const admin = require(`./routes/admin`);
const bug = require(`./routes/bug`);
const api = require(`./routes/api`);
const login = require(`./routes/login`);

app.use(
session({ secret: "trackpath", resave: false, saveUninitialized: true })
);
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use((req, res, next) => {
console.log("ACESSANDO MIDDLEWARE...");
Expand All @@ -36,6 +41,7 @@ app.use((req, res, next) => {

res.locals.success_msg = req.flash("success_msg");
res.locals.error_msg = req.flash("error_msg");
res.locals.error = req.flash("error");

next();
});
Expand All @@ -56,6 +62,11 @@ app.use(`/feature`, feature);
app.use(`/admin`, admin);
app.use(`/bug`, bug);
app.use(`/api`, api);
app.use(`/login`, login);

app.get("/", (req, res) => {
res.redirect("/login");
});

app.listen(SERVER_PORT, () => {
console.log("Server listening on port " + SERVER_PORT);
Expand Down
40 changes: 40 additions & 0 deletions config/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//import localStrategy from `passport-local`;
const localStrategy = require(`passport-local`).Strategy;
const bcrypt = require(`bcryptjs`);
const User = require(`../models/User`);

module.exports = (passport) => {
passport.use(
new localStrategy({ usernameField: `email` }, (email, password, done) => {
User.findByEmail(email)
.then((user) => {
if (!user) {
return done(null, false, { message: "account not found" });
}

bcrypt.compare(password, user.password, (err, samePsw) => {
if (samePsw) {
return done(null, user);
} else {
return done(null, false, { message: "Incorrect password" });
}
});
})
.catch(() => {});
})
);

passport.serializeUser((user, done) => {
done(null, user.id);
});

passport.deserializeUser((id, done) => {
User.findByPk(id)
.then((user) => {
done(null, user);
})
.catch((e) => {
console.log("err: " + e);
});
});
};
4 changes: 2 additions & 2 deletions models/Bug.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const create = (bug) => {
let sql = `INSERT INTO bugs
(Title, Summary, EstimatedHours, DeliveryDate, CreatedAt, ProjectID, StatusID, SeverityID, PriorityID)
VALUES
("${bug.Title}", "${bug.Summary}", "${bug.EstimatedHours}", STR_TO_DATE('${bug.DeliveryDate}', '%Y-%m-%d'),
STR_TO_DATE('${bug.CreatedAt}', '%Y-%m-%d'), "${bug.ProjectID}", "${bug.StatusID}", "${bug.SeverityID}", "${bug.PriorityID}")`;
("${bug.Title}", "${bug.Summary}", "${bug.EstimatedTime}", STR_TO_DATE('${bug.DeliveryDate}', '%Y-%m-%d'),
STR_TO_DATE('${bug.CreatedAt}', '%Y-%m-%d'), "${bug.projectID}", "${bug.StatusID}", "${bug.SeverityID}", "${bug.PriorityID}")`;

connection.query(sql, (err, result) => {
err ? reject() : resolve(result);
Expand Down
38 changes: 38 additions & 0 deletions models/User.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const connection = require("../db/connection");
const mysql = require("mysql");

const create = (user) => {
return new Promise((resolve, reject) => {
sql = `insert into users (name, email, isAdmin, password ) values ('${user.name}', '${user.email}', ${user.isAdmin}, '${user.password}')`;

connection.query(sql, (err, result) => {
err ? reject(err) : resolve(result);
});
});
};

const findByEmail = (email) => {
return new Promise((resolve, reject) => {
sql = `select * from users where email = ${mysql.escape(email)}`;

connection.query(sql, (err, result) => {
err ? reject(err) : resolve(result[0]);
});
});
};

const findByPk = (id) => {
return new Promise((resolve, reject) => {
sql = `select * from users where id = ${mysql.escape(id)}`;

connection.query(sql, (err, result) => {
err ? reject(err) : resolve(result[0]);
});
});
};

module.exports = {
create: create,
findByEmail: findByEmail,
findByPk: findByPk,
};
32 changes: 32 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"main": "app.js",
"dependencies": {
"@handlebars/allow-prototype-access": "^1.0.3",
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"connect-flash": "^0.1.1",
"express": "^4.17.1",
Expand All @@ -12,6 +13,8 @@
"moment": "^2.26.0",
"mysql": "^2.18.1",
"mysql2": "^2.1.0",
"passport": "^0.4.1",
"passport-local": "^1.0.0",
"pg": "^8.2.1",
"pg-hstore": "^2.3.3",
"sequelize": "^5.21.11"
Expand Down
107 changes: 107 additions & 0 deletions public/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,19 @@ hr {
margin-left: 50px;
}

.solveItemDivNotFound {
display: flex;
justify-content: center;
background-color: rgb(228, 228, 228);
color: #444;
width: 100%;
padding: 9px;
}

.solveItemDivNotFound p {
padding: 20px;
}

/* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
.active,
.solveItemDiv:hover {
Expand Down Expand Up @@ -292,3 +305,97 @@ hr {
.i-size {
font-size: 50px;
}

.nav-bar-menu {
display: flex;
justify-content: space-between;
}

.login-access {
display: flex;
justify-content: space-between;
text-decoration: none;
}

.login-access h4 {
margin-right: 8px;
}

.login-access i {
font-size: 22px;
margin-top: 14px;
margin-right: 15px;
}

/* Bordered form */
.login form {
border: 2px solid rgb(206, 206, 206);
}

/* Full-width inputs */
.login input[type="email"],
input[type="password"] {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
box-sizing: border-box;
}

/* Set a style for all buttons */
.login-button {
background-color: #808381;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
cursor: pointer;
width: 100%;
}

/* Add a hover effect for buttons */
.login-button:hover {
opacity: 0.8;
}

/* Extra style for the cancel button (red) */
.signup {
width: auto;
padding: 10px 18px;
background-color: #06c05a;
}

/* Center the avatar image inside this container */
.imgcontainer {
text-align: center;
margin: 24px 0 12px 0;
}

/* Avatar image */
img.avatar {
width: 40%;
border-radius: 50%;
}

/* Add padding to containers */
.login-form-container {
padding: 16px;
}

/* The "Forgot password" text */
span.psw {
float: right;
padding-top: 16px;
}

/* Change styles for span and cancel button on extra small screens */
@media screen and (max-width: 300px) {
span.psw {
display: block;
float: none;
}
.signup {
width: 100%;
}
}
Binary file removed public/img/avatar1.png
Binary file not shown.
Binary file removed public/img/avatar2.png
Binary file not shown.
Binary file removed public/img/avatar3.png
Binary file not shown.
Binary file removed public/img/avatar5.png
Binary file not shown.
Binary file removed public/img/avatar6.png
Binary file not shown.
Binary file added public/img/login.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed public/img/region.jpg
Binary file not shown.
12 changes: 10 additions & 2 deletions public/js/lib.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
function w3_open() {
document.getElementById("main").style.marginLeft = "25%";
document.getElementById("mySidebar").style.width = "25%";
document.getElementById("main").style.marginLeft = "23%";
document.getElementById("mySidebar").style.width = "23%";
document.getElementById("mySidebar").style.display = "block";
document.getElementById("openNav").style.display = "none";
localStorage.setItem("openSidebar", true);
}

function w3_close() {
document.getElementById("main").style.marginLeft = "0%";
document.getElementById("mySidebar").style.display = "none";
document.getElementById("openNav").style.display = "inline-block";
localStorage.setItem("openSidebar", false);
}

if (localStorage.getItem("openSidebar") === "true") {
w3_open();
} else {
w3_close();
}
15 changes: 3 additions & 12 deletions routes/bug.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,8 @@ router.get(`/add/:projectID`, (req, res) => {
});

router.post(`/add`, (req, res) => {
const bug = {
Title: req.body.title,
Summary: req.body.Summary,
EstimatedHours: req.body.estimatedTime,
DeliveryDate: req.body.DeliveryDate,
CreatedAt: moment().format("YYYY-MM-DD"),
ProjectID: req.body.projectID,
StatusID: req.body.featureStatus,
SeverityID: req.body.selectSeverity,
PriorityID: req.body.selectPriority,
};
const bug = req.body;
bug.CreatedAt = moment().format("YYYY-MM-DD");

Bug.create(bug)
.then(() => {
Expand Down Expand Up @@ -63,7 +54,7 @@ router.get(`/solveIssue/:bugID`, (req, res) => {
});

router.post(`/solveIssue`, (req, res) => {
let bug = req.body;
const bug = req.body;

Bug.updateSolveIssue(bug)
.then(() => {
Expand Down
Loading

0 comments on commit 575c765

Please sign in to comment.