222 lines
7.0 KiB
JavaScript
222 lines
7.0 KiB
JavaScript
//import mariadb
|
|
import ownConn from "../dbConfig.js";
|
|
|
|
import bcrypt from "bcryptjs";
|
|
import jwt from "jsonwebtoken";
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
import {
|
|
getUsers,
|
|
getUserById,
|
|
updateUserById,
|
|
deleteUserById,
|
|
getSelectedUsersByUser,
|
|
} from "../models/usersModel.js";
|
|
|
|
//insert user to databased
|
|
export const signUp = async (req, res, next) => {
|
|
try {
|
|
let sql = `SELECT id FROM users WHERE LOWER(username) = LOWER(?)`;
|
|
const result = await ownConn.execute(sql, [req.body.username])
|
|
|
|
if (result.length > 0) { //username already exists
|
|
return res.status(409).send({
|
|
message: 'This username is already in use!'
|
|
});
|
|
} else { // username not in use
|
|
bcrypt.hash(req.body.password, 10, (err, hash) => {
|
|
if (err) {
|
|
throw err;
|
|
return res.status(500).send({
|
|
message: err,
|
|
});
|
|
} else {
|
|
const currentTime = new Date();
|
|
const options = {
|
|
timeZone: 'Europe/Berlin',
|
|
year: 'numeric',
|
|
month: '2-digit',
|
|
day: '2-digit',
|
|
hour: '2-digit',
|
|
minute: '2-digit'
|
|
};
|
|
const dateTimeString = currentTime.toLocaleString('de-DE', options);
|
|
const results = ownConn.query(`INSERT INTO users(id, username, password, registered, fullName, email, phonenumber, address, city, postcode, adminBool, technicianBool, readerBool) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
[
|
|
uuidv4(),
|
|
req.body.username,
|
|
hash,
|
|
dateTimeString,
|
|
req.body.fullName,
|
|
req.body.email,
|
|
req.body.phonenumber,
|
|
req.body.address,
|
|
req.body.city,
|
|
req.body.postcode,
|
|
req.body.adminBool,
|
|
req.body.technicianBool,
|
|
req.body.readerBool,
|
|
]);
|
|
return res.status(201).send({
|
|
message: "Registered!",
|
|
})
|
|
}
|
|
})
|
|
}
|
|
}
|
|
catch (err) {
|
|
// Manage Errors
|
|
console.log("SQL error : ", err);
|
|
result(err, null);
|
|
}
|
|
};
|
|
|
|
// Update user password to Database
|
|
export const updatePasswordById = async (req, res, next) => {
|
|
try {
|
|
const id = req.body.id;
|
|
let sql = `UPDATE users SET password = ? WHERE id = ?`;
|
|
bcrypt.hash(req.body.password, 10, async (err, hash) => {
|
|
if (err) {
|
|
throw err;
|
|
return res.status(500).send({
|
|
message: err,
|
|
});
|
|
} else {
|
|
const results = await ownConn.query(sql, [hash, id]);
|
|
return res.status(200).send({
|
|
message: 'Password changed!',
|
|
});
|
|
}
|
|
});
|
|
}
|
|
catch (err) {
|
|
// Manage Errors
|
|
console.log("SQL error : ", err);
|
|
result(err, null);
|
|
}
|
|
};
|
|
|
|
export const login = async (req, res, next) => {
|
|
try {
|
|
let sql = `SELECT * FROM users WHERE username = ?`;
|
|
const result = await ownConn.execute(sql, [req.body.username]);
|
|
if (!result.length) {
|
|
return res.status(400).send({
|
|
message: 'Username or password incorrect!',
|
|
});
|
|
}
|
|
bcrypt.compare(
|
|
req.body.password,
|
|
result[0]['password'],
|
|
async (bErr, bResult) => {
|
|
if (bErr) {
|
|
return res.status(400).send({
|
|
message: 'Username or password incorrect!',
|
|
});
|
|
}
|
|
if (bResult) {
|
|
// password match
|
|
const authtoken = jwt.sign(
|
|
{
|
|
username: result[0].username,
|
|
userId: result[0].id,
|
|
},
|
|
'SECRETTUEITKEY',
|
|
{ expiresIn: '300s' } // 5min
|
|
);
|
|
const refreshtoken = jwt.sign(
|
|
{
|
|
username: result[0].username,
|
|
userId: result[0].id,
|
|
},
|
|
'SECRETTUEITKEY',
|
|
{ expiresIn: '7d' }
|
|
);
|
|
let sql1 = `UPDATE users SET lastLogin = ? WHERE id = ?`;
|
|
const currentTime = new Date();
|
|
const options = {
|
|
timeZone: 'Europe/Berlin',
|
|
year: 'numeric',
|
|
month: '2-digit',
|
|
day: '2-digit',
|
|
hour: '2-digit',
|
|
minute: '2-digit'
|
|
};
|
|
const dateTimeString = currentTime.toLocaleString('de-DE', options);
|
|
const results = await ownConn.query(sql1, [dateTimeString, result[0].id]);
|
|
return res.status(200).send({
|
|
message: 'Logged in!',
|
|
token: { authToken: authtoken, refreshToken: refreshtoken },
|
|
user: result[0],
|
|
});
|
|
}
|
|
return res.status(400).send({
|
|
message: 'Username or password incorrect!',
|
|
});
|
|
}
|
|
);
|
|
}
|
|
catch (err) {
|
|
// Manage Errors
|
|
console.log("SQL error : ", err);
|
|
}
|
|
};
|
|
|
|
//get all users
|
|
export const showUsers = (req, res) => {
|
|
getUsers((err, results) => {
|
|
if (err) {
|
|
res.send(err);
|
|
} else {
|
|
res.json(results);
|
|
}
|
|
});
|
|
};
|
|
|
|
//get single user by id
|
|
export const showUserById = (req, res) => {
|
|
getUserById(req.params.id, (err, results) => {
|
|
if (err) {
|
|
res.send(err);
|
|
} else {
|
|
res.json(results);
|
|
}
|
|
});
|
|
};
|
|
|
|
//get all selected users by user
|
|
export const showSelectedUsersByUser = (req, res) => {
|
|
getSelectedUsersByUser(req.params.id, (err, results) => {
|
|
if (err) {
|
|
res.send(err);
|
|
} else {
|
|
res.json(results);
|
|
}
|
|
});
|
|
};
|
|
|
|
|
|
// Update user
|
|
export const updateUser = (req, res) => {
|
|
const data = req.body;
|
|
updateUserById(data, (err, results) => {
|
|
if (err) {
|
|
res.send(err);
|
|
} else {
|
|
res.json(results);
|
|
}
|
|
});
|
|
};
|
|
|
|
// Delete user
|
|
export const deleteUser = (req, res) => {
|
|
const id = req.params.id;
|
|
deleteUserById(id, (err, results) => {
|
|
if (err) {
|
|
res.send(err);
|
|
} else {
|
|
res.json(results);
|
|
}
|
|
});
|
|
}; |