Skip to content

Commit

Permalink
authentication updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Henrique Sagara authored and Henrique Sagara committed Feb 23, 2024
1 parent e386e99 commit 7a69a7a
Show file tree
Hide file tree
Showing 10 changed files with 202 additions and 30 deletions.
5 changes: 5 additions & 0 deletions modules/data-service-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ async function isUserIdExists(userId) {
return !!result;
}

async function getUserByEmail(email) {
const result = await client.db(dbName).collection(userCollection).findOne({email: email})
}

async function hashPassword(password){
return await bcrypt.hash(password, saltRounds)
}

export {
createUser,
isUserIdExists,
getUserByEmail,
};
15 changes: 14 additions & 1 deletion modules/database.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import express from "express";
import path from "path";
import { MongoClient } from "mongodb";
import dotenv from "dotenv";
dotenv.config();
Expand All @@ -8,6 +10,15 @@ const dbName = "timesheet_app";
const collectionName = "hoursRecords";
const userCollection = "users"

const authenticateUser = (req, res, next) => {
if(req.session.user) {
next()
}
else {
res.status(401).json({message: 'Unauthorized'});
}
}

// Function to connect to the MongoDB server
async function connectToDatabase() {
try {
Expand All @@ -29,4 +40,6 @@ async function listDatabases(client) {
});
}

export { client, dbName, collectionName, userCollection, listDatabases, connectToDatabase };


export { client, dbName, collectionName, userCollection, listDatabases, connectToDatabase, authenticateUser };
63 changes: 58 additions & 5 deletions node_modules/.package-lock.json

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

65 changes: 59 additions & 6 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"date-fns": "^3.3.0",
"dotenv": "^16.4.1",
"express": "^4.18.2",
"express-session": "^1.18.0",
"mongodb": "^6.3.0",
"mongoose": "^8.1.0",
"react": "^18.2.0",
Expand Down
55 changes: 46 additions & 9 deletions routes/root.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import express from "express";
import path from "path";
import { client } from "../modules/database.js";
import bcrypt from 'bcrypt'
import { client, authenticateUser } from "../modules/database.js";
import { timeIn, timeOut, deleteUserById, addNewUser } from "../modules/data-service.js";
import { createUser } from "../modules/data-service-auth.js";
import { createUser, getUserByEmail } from "../modules/data-service-auth.js";


const router = express.Router();
const currentDir = process.cwd();



// Route to handle creating a new user
router.post("/create-user", async (req, res) => {
const newUser = req.body;
Expand All @@ -25,6 +28,37 @@ router.post("/create-user", async (req, res) => {
}
});

router.post("/login", async (req, res) => {
const {email, password} = req.body;

try {
const user = await getUserByEmail(email)
console.log(user)

if(!user) {
res.status(401).json({message: 'Invalid email or password'});
return;
}

const passwordMatch = await bcrypt.compare(password, user.password);

if(passwordMatch) {
req.session.user = {
id: user._id,
email: user.email,
}
res.status(200).json({message: 'Login successful'});
}
else {
res.status(401).json({ message: 'Invalid email or password' });
}

} catch (error) {
console.error('Error during login:', error);
res.status(500).json({ message: 'Internal Server Error' });
}
})

router.delete("/delete/:userId", async (req, res) => {
const userId = req.params.userId;

Expand All @@ -40,16 +74,19 @@ router.delete("/delete/:userId", async (req, res) => {
}
});

router.get("/time-in", (req, res) => {
res.sendFile(path.resolve(currentDir, 'views', 'time-in.html'));
router.get('/', (req, res) => {
res.sendFile(path.resolve(currentDir, 'views', 'index.html'));
});

router.get("/time-out", (req, res) => {
res.sendFile(path.resolve(currentDir, 'views', 'time-out.html'))
router.get('/shift-table', authenticateUser, (req, res) => {
res.sendFile(path.resolve(currentDir, 'views', 'shiftTable.html'))
})

router.get('/', (req, res) => {
res.sendFile(path.resolve(currentDir, 'views', 'index.html'));
});
router.get('/shift-tracker', authenticateUser, (req, res) => {
res.sendFile(path.resolve(currentDir, 'views', 'shiftTracker.html'))
})

router.get('/about-us', (req, res) => {
res.sendFile(path.resolve(currentDir, 'views', 'aboutUs.html'))
})
export default router;
6 changes: 3 additions & 3 deletions routes/time-in.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import express from "express";
import path from "path";
import { client } from "../models/database.js";
import { client, authenticateUser } from "../models/database.js";
import { timeIn } from "../models/data-service.js";

const router = express.Router();
const currentDir = process.cwd();

router.post("/time-in", async (req, res) => {
router.post("/time-in", authenticateUser, async (req, res) => {
const userId = req.body.userId;

try {
Expand All @@ -19,7 +19,7 @@ router.post("/time-in", async (req, res) => {
});


router.get("/time-in", (req, res) => {
router.get("/time-in", authenticateUser, (req, res) => {
res.sendFile(path.resolve(currentDir, 'views', 'time-in.html'));
});

Expand Down
4 changes: 2 additions & 2 deletions routes/time-out.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { timeOut } from "../models/data-service.js";
const router = express.Router();
const currentDir = process.cwd();

router.post("/time-out", async (req, res) => {
router.post("/time-out", authenticateUser, async (req, res) => {
const userId = req.body.userId;

try {
Expand All @@ -18,7 +18,7 @@ router.post("/time-out", async (req, res) => {
}
});

router.get("/time-out", (req, res) => {
router.get("/time-out", authenticateUser, (req, res) => {
res.sendFile(path.resolve(currentDir, 'views', 'time-out.html'))
})

Expand Down
Loading

0 comments on commit 7a69a7a

Please sign in to comment.