Skip to content

Commit

Permalink
feat: implement login page & employee pages
Browse files Browse the repository at this point in the history
Resolves #57
Resolves #55
Resolves #54
Resolves #41
Resolves #40
Resolves #39
Resolves #37
Resolves #35
Resolves #5
  • Loading branch information
LiamTownsley2 committed Nov 14, 2023
2 parents 9cbf90a + c686290 commit c0ce075
Show file tree
Hide file tree
Showing 21 changed files with 869 additions and 172 deletions.
87 changes: 83 additions & 4 deletions electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const mongo = require('mongodb');
const express = require('express');
const cors = require('cors');
const bodyParser = require('body-parser')
const bcrypt = require('bcrypt');

const server = express();
server.use(cors());
Expand All @@ -25,7 +26,7 @@ export async function connectToMongoDB() {
try {
await client.connect();
console.log('Connected to MongoDB');
const _db = client.db('uni-project');
const _db = client.db('uni-project-dev');

database = _db;
return _db;
Expand All @@ -38,6 +39,47 @@ export async function connectToMongoDB() {
}
}

server.get('/api/signup', async (req: any, res: any) => {
const db = await connectToMongoDB();
const collection = db.collection('employees');
const { first_name, last_name, email, password, department } = req.query;

bcrypt.hash(password, 10, (err: any, hash: any) => {
res.send(JSON.stringify(data));
console.log('password hashed');
console.log(`${password} -> ${hash}`);
});
});

server.post('/api/login', async (req: any, res: any) => {
try {
const db = await connectToMongoDB();
const collection = db.collection('employees');

const body = JSON.parse(Object.keys(req.body)[0]);
const email = body.email;
const password = body.password;
if (!email || !password) throw new Error("Required fields not provided.");
console.log(`
Email: ${email}
Password: ${password}`)
const data = await collection.findOne({ email });
console.log(data);
if (!data) throw new Error("User not found in database.");

const crypt = await bcrypt.compare(password, data.password);
if (!crypt) {
res.status(400)
return res.json({ status: false })
}
return res.json({ status: true })
} catch (error) {
console.log(error);
res.status(400);
return res.json({ status: false })
}
});

server.get('/api/assets', async (_: any, res: any) => {
const db = await connectToMongoDB();
const collection = db.collection('assets');
Expand All @@ -46,6 +88,8 @@ server.get('/api/assets', async (_: any, res: any) => {
res.json(data);
});



server.get('/api/asset/:id', async (req: any, res: any) => {
const db = await connectToMongoDB();
const collection = db.collection('assets');
Expand All @@ -56,6 +100,34 @@ server.get('/api/asset/:id', async (req: any, res: any) => {
res.json(data);
});

server.get('/api/employees', async (_: any, res: any) => {
const db = await connectToMongoDB();
const collection = db.collection('employees');

const data = await collection.find().toArray();
res.json(data);
});

server.get('/api/employee/:id', async (req: any, res: any) => {
const db = await connectToMongoDB();
const collection = db.collection('employees');
const id = req.params.id;
console.log('api/employee/id -> ', id);
const data = await collection.findOne({ _id: new mongo.ObjectId(id) });
console.log(data);
res.json(data);
});

server.get('/api/employee/:id/assets', async (req: any, res: any) => {
const db = await connectToMongoDB();
const collection = db.collection('assets');
const id = req.params.id;
console.log('api/employee/id -> ', id);
const data = await collection.find({ employee: new mongo.ObjectId(id) }).toArray();
console.log(data);
res.json(data);
});

server.delete('/api/delete-all-assets', async (_: any, res: any) => {
const db = await connectToMongoDB();
const collection = db.collection('assets');
Expand All @@ -79,6 +151,8 @@ server.get('/api/assets/create', async (req: any, _: any) => {
const db = await connectToMongoDB();
const collection = db.collection('assets');
const { name, type, model, manufacturer, ip, date, note, employee } = req.query;
const _employee = new mongo.ObjectId(employee);

collection.insertOne({
name,
type,
Expand All @@ -87,7 +161,7 @@ server.get('/api/assets/create', async (req: any, _: any) => {
ip,
date,
note,
employee
employee: _employee
})
})
// ===============================================================
Expand Down Expand Up @@ -115,16 +189,21 @@ function createWindow() {
preload: path.join(__dirname, 'preload.js'),
},
width: 1500,
height: 1000
height: 1000,
show: false
})

win.removeMenu();
// win.removeMenu();

// Test active push message to Renderer-process.
win.webContents.on('did-finish-load', () => {
win?.webContents.send('main-process-message', (new Date).toLocaleString())
})

win.once('ready-to-show', () => {
win?.show();
});

if (VITE_DEV_SERVER_URL) {
win.loadURL(VITE_DEV_SERVER_URL)
} else {
Expand Down
Loading

0 comments on commit c0ce075

Please sign in to comment.