-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuser.js
106 lines (90 loc) · 4.28 KB
/
user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import express from 'express';
import sqlite3 from 'sqlite3';
import jwt from 'jsonwebtoken';
import { SECRET, SALT } from './config.js';
import bcrypt from 'bcrypt';
const router = express.Router();
const db = new sqlite3.Database('branchEvent.db');
// A single Login endpoint for both student and company, returns token and userdata
router.post('/login', (req, res) => {
const { email, password } = req.body;
const companyQuery = 'SELECT * FROM Company WHERE email = ?';
const studentQuery = 'SELECT * FROM Student WHERE email = ?';
// Check company table
db.get(companyQuery, [email], (companyErr, companyResult) => {
if (companyErr) {
console.error('Error querying company table:', companyErr);
return res.status(500).json({ error: 'Internal Server Error' });
}
if (companyResult) {
// User found in company table
bcrypt.compare(password, companyResult.password, (bcryptErr, bcryptResult) => {
if (bcryptErr) {
console.error('Error comparing passwords:', bcryptErr);
return res.status(500).json({ error: 'Internal Server Error' });
}
if (bcryptResult) {
// Passwords match - company authenticated
console.log('Company authenticated successfully');
const token = jwt.sign({ id: companyResult.id, userType: 'company' }, SECRET, { expiresIn: 864000 });
return res.status(200).json({ token, userType: 'company', userData: companyResult });
} else {
// Incorrect password
console.log('Incorrect password for company');
return res.status(401).json({ error: 'Incorrect email or password' });
}
});
} else {
// User not found in company table, check student table
db.get(studentQuery, [email], (studentErr, studentResult) => {
if (studentErr) {
console.error('Error querying student table:', studentErr);
return res.status(500).json({ error: 'Internal Server Error' });
}
if (studentResult) {
// User found in student table
bcrypt.compare(password, studentResult.password, (bcryptErr, bcryptResult) => {
if (bcryptErr) {
console.error('Error comparing passwords:', bcryptErr);
return res.status(500).json({ error: 'Internal Server Error' });
}
if (bcryptResult) {
// Passwords match - student authenticated
console.log('Student authenticated successfully');
const token = jwt.sign({ id: studentResult.id, userType: 'student' }, SECRET, { expiresIn: 864000 });
return res.status(200).json({ token, userType: 'student', userData: studentResult });
} else {
// Incorrect password
console.log('Incorrect password for student');
return res.status(401).json({ error: 'Incorrect email or password' });
}
});
} else {
// User not found in student table either
console.log('User not found');
return res.status(401).json({ error: 'Incorrect email or password' });
}
});
}
});
});
router.get('/company/:companyId/tags', (req, res) => {
const companyId = req.params.companyId;
// SQLite query to retrieve tag IDs associated with the given company ID
const query = `
SELECT tag_id
FROM Company_tags
WHERE company_id = ?;
`;
db.all(query, [companyId], (err, rows) => {
if (err) {
console.error('Error retrieving tags:', err);
res.status(500).json({ error: 'Internal server error' });
} else {
// Extract tag IDs from the query result
const tagIds = rows.map(row => row.tag_id);
res.json(tagIds);
}
});
});
export default router;