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

feat: implement login page & employee pages #58

Merged
merged 7 commits into from
Nov 14, 2023
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
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