Skip to content

Commit

Permalink
Merge pull request #16 from WildCodeSchool-2023-09/login
Browse files Browse the repository at this point in the history
Login
  • Loading branch information
aimach authored Jan 24, 2024
2 parents 1f6052d + bad6263 commit e8b5d15
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 8 deletions.
4 changes: 2 additions & 2 deletions backend/seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const seed = async () => {
);
}

for (let i = 0; i <= 35; i += 1) {
for (let i = 0; i <= 20; i += 1) {
queries.push(
database.query(
"insert into player(role_id,username, email, password) values (?,?,?,?)",
Expand Down Expand Up @@ -86,7 +86,7 @@ const seed = async () => {
);
}

for (let i = 0; i <= 50; i += 1) {
for (let i = 0; i <= 25; i += 1) {
const startDate = faker.date.past({ years: 1 });
const d = new Date(startDate);
queries.push(
Expand Down
29 changes: 27 additions & 2 deletions backend/src/controllers/playerControllers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const argon2 = require("argon2");
const tables = require("../tables");

// The B of BREAD - Browse (Read All) operation
const browse = async (req, res, next) => {
try {
Expand Down Expand Up @@ -42,7 +42,6 @@ const destroy = async (req, res, next) => {
const add = async (req, res, next) => {
// Extract the item data from the request body
const player = req.body;

try {
// Insert the item into the database
const insertId = await tables.player.create(player);
Expand All @@ -55,8 +54,34 @@ const add = async (req, res, next) => {
}
};

const log = async (req, res, next) => {
try {
const login = await tables.player.readEmail(req.body.email);

if (login) {
const passwordMatch = await argon2.verify(
login.password,
req.body.password
);

if (passwordMatch) {
res.status(200).json({
message: "Login successful",
});
} else {
res.sendStatus(403);
}
} else {
res.sendStatus(403);
}
} catch (err) {
next(err);
}
};

module.exports = {
browse,
destroy,
add,
log,
};
8 changes: 8 additions & 0 deletions backend/src/models/PlayerManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,13 @@ class PlayerManager extends AbstractManager {

return result.insertId;
}

async readEmail(mail) {
const [rows] = await this.database.query(
`select * from ${this.table} where email = ?`,
[mail]
);
return rows[0];
}
}
module.exports = PlayerManager;
2 changes: 2 additions & 0 deletions backend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const PlayerControllers = require("./controllers/playerControllers");
const PartyControllers = require("./controllers/partyControllers");
const GameControllers = require("./controllers/gameControllers");
const ContactControllers = require("./controllers/contactControllers");
// const AuthControllers = require("./controllers/authControllers");

const hashPassword = require("./midleware/hash/hashPassword");
const validateUsers = require("./midleware/joi/validateUsers");
Expand All @@ -16,5 +17,6 @@ router.get("/party", PartyControllers.browse);
router.get("/games", GameControllers.browse);
router.post("/contact", ContactControllers.send);
router.post("/players", validateUsers, hashPassword, PlayerControllers.add);
router.post("/login", PlayerControllers.log);

module.exports = router;
2 changes: 2 additions & 0 deletions backend/src/tables.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ const GameManager = require("./models/GameManager");
const ItemManager = require("./models/ItemManager");
const PartyManager = require("./models/PartyManager");
const PlayerManager = require("./models/PlayerManager");
// const AuthManager = require("./models/AuthManager");

const managers = [
ItemManager,
PartyManager,
PlayerManager,
GameManager,
// AuthManager,
// Add other managers here
];

Expand Down
4 changes: 0 additions & 4 deletions frontend/src/main.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ const router = createBrowserRouter([
path: "tictactoe",
element: <TicTacToe />,
},
{
path: "signup",
element: <Signup />,
},
],
},
{
Expand Down

0 comments on commit e8b5d15

Please sign in to comment.