wf-radio/src/services/authService.js

85 lines
3.1 KiB
JavaScript
Raw Normal View History

const userModel = require("../models/user");
const AppError = require("../utils/appError");
2025-05-04 23:14:00 +02:00
const cryptoUtils = require("../utils/crypto");
const configManager = require("../utils/configManager");
const validate = require("../utils/validate_legacy");
const JWT_Secret = configManager.getJWTSecret();
2025-05-04 23:14:00 +02:00
async function login(username, email, password) {
// Check for null
2025-05-04 23:14:00 +02:00
if (!(username || email) || !password) {
throw new AppError(400, "Bad request", "missing credentials");
}
// Get user data
2025-05-04 23:14:00 +02:00
let user_get;
if (email) { // If matches email
user_get = await userModel.getUserByEmail(email);
} else
2025-05-04 23:14:00 +02:00
if (username) { // if matches username
user_get = await userModel.getUserByName(username);
} else {
2025-05-04 23:14:00 +02:00
console.debug("Failed finding user, weird...")
throw new AppError(401, "Unauthorized", "Invalid credentials");
}
// Check if user exists
2025-05-04 23:14:00 +02:00
if (!user_get || user_get.length == 0) {
// throw new AppError(401, "Unauthorized: No user with this name");
throw new AppError(401, "Unauthorized", "Invalid credentials");
}
// Just in case
2025-05-04 23:14:00 +02:00
if (user_get.length > 1) {
throw new AppError(500, "Internal server error", "Found multiple users with this name or email, please contact administration");
}
2025-05-04 23:14:00 +02:00
const user = user_get[0];
// Get user password
const saved_password_get = await userModel.getUserPassword(user.username);
// Check if retrieved password sucessfully
if (!saved_password_get || saved_password_get.length == 0) {
throw new AppError(500, "Unable to retrieve user password");
}
saved_password = saved_password_get[0].password;
// Check if retrieved password sucessfully again
if (!saved_password) {
throw new AppError(500, "Unable to retrieve user password");
}
// Check if passwords match
2025-05-04 23:14:00 +02:00
const passwords_match = await cryptoUtils.passwordsMatch(password, saved_password)
if (!passwords_match) {
// throw new AppError(401, "Unauthorized: Invalid password");
2025-05-04 23:14:00 +02:00
console.debug(password, "differs from", saved_password);
throw new AppError(401, "Unauthorized", "Invalid credentials");
}
2025-05-04 23:14:00 +02:00
const payload = { type: "user",
username: user.username,
email: user.email,
role: user.role };
const token = await cryptoUtils.signToken(payload);
return token;
// // Check if passwords match
// const passwords_match = await bcrypt.compare(password, user[0].password);
// if (!passwords_match) {
// // throw new AppError(401, "Unauthorized: Invalid password");
// console.debug("Password doesn't match")
// throw new AppError(401, "Unauthorized", "Invalid credentials");
// }
// return jwt.sign({ username: user[0].username, role: user[0].role }, await JWT_Secret);
}
// function authorizeRole(user, roles) {
// if (!user || !roles.includes(user.role)) {
// throw new AppError(401, "Unauthorized: You don't have the necessary permissions to access this resource");
// }
// }
2025-04-25 14:06:03 +02:00
module.exports = { login };