routing improved, settings initialized

This commit is contained in:
Torsten Schulz
2024-07-21 18:47:45 +02:00
parent 12d66d6f9c
commit cd0699f3fd
23 changed files with 476 additions and 69 deletions

View File

@@ -4,24 +4,30 @@ import User from '../models/community/user.js';
import UserParam from '../models/community/user_param.js';
import UserParamType from '../models/type/user_param.js';
import { sendAccountActivationEmail, sendPasswordResetEmail } from './emailService.js';
import { sequelize } from '../utils/sequelize.js';
import { encrypt, generateIv } from '../utils/encryption.js';
const saltRounds = 10;
export const registerUser = async ({ email, username, password, language }) => {
const [results] = await sequelize.query(
'SELECT * FROM "community"."user" WHERE pgp_sym_decrypt("email", :key) = :email',
const iv = generateIv();
const encryptedEmail = encrypt(email, iv);
const results = await sequelize.query(
`SELECT * FROM "community"."user" WHERE public.pgp_sym_decrypt("email"::bytea, :key::text) = :email`,
{
replacements: { key: process.env.SECRET_KEY, email },
type: sequelize.QueryTypes.SELECT
}
);
if (results.length > 0) {
console.log(results);
if (results.length && results.length > 0) {
throw new Error('Email already in use');
}
const iv = generateIv();
const encryptedEmail = encrypt(email, iv);
const hashedPassword = await bcrypt.hash(password, saltRounds);
const resetToken = uuidv4();
const user = await User.create({
email: encryptedEmail,
iv: iv.toString('hex'),
@@ -31,17 +37,21 @@ export const registerUser = async ({ email, username, password, language }) => {
active: false,
registration_date: new Date()
});
const languageType = await UserParamType.findOne({ where: { description: 'language' } });
if (!languageType) {
throw new Error('Language type not found');
}
await UserParam.create({
userId: user.id,
paramTypeId: languageType.id,
value: language
});
const activationLink = `${process.env.FRONTEND_URL}/activate?token=${resetToken}`;
await sendAccountActivationEmail(email, activationLink, username, resetToken, language);
return { id: user.hashedId, username: user.username, active: user.active };
};
@@ -52,10 +62,22 @@ export const loginUser = async ({ username, password }) => {
throw new Error('credentialsinvalid');
}
const match = await bcrypt.compare(password, user.password);
console.log(match, password, user.password, await bcrypt.hash(password, saltRounds));
if (!match) {
throw new Error('credentialsinvalid');
}
return { id: user.hashedId, username: user.username, active: user.active };
const neededParams = await UserParam.findAll({
where: {
userId: user.id
},
include: {
model: UserParamType,
where: {
description: ['birthdate', 'gender']
}
}
});
return { id: user.hashedId, username: user.username, active: user.active, forwardDataInput: neededParams.length < 2 };
};
export const handleForgotPassword = async ({ email }) => {