import bcrypt from 'bcrypt'; import { v4 as uuidv4 } from 'uuid'; 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 '../services/emailService.js'; import i18n from '../utils/i18n.js'; const saltRounds = 10; export const register = async (req, res) => { const { email, username, password, language } = req.body; try { const hashedPassword = await bcrypt.hash(password, saltRounds); const resetToken = uuidv4(); const user = await User.create({ email, username, password: hashedPassword, resetToken: resetToken, active: false, registration_date: new Date() }); const languageType = await UserParamType.findOne({ where: { description: 'language' } }); if (!languageType) { return res.status(500).json({ error: 'Language type not found' }); } console.log(user.id, languageType.id); 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); res.status(201).json({ id: user.hashedId, username: user.username, active: user.active }); } catch (error) { console.log(error); res.status(500).json({ error: error.message }); } }; export const login = async (req, res) => { const { email, password } = req.body; try { const user = await User.findOne({ where: { email } }); if (!user) { return res.status(401).json({ error: 'Invalid email or password' }); } if (!user.active) { return res.status(403).json({ error: 'Account not activated' }); } const match = await bcrypt.compare(password, user.password); if (!match) { return res.status(401).json({ error: 'Invalid email or password' }); } res.status(200).json({ id: user.hashed_id, username: user.username }); } catch (error) { res.status(500).json({ error: 'Error logging in' }); } }; export const forgotPassword = async (req, res) => { const { email } = req.body; try { const user = await User.findOne({ where: { email } }); if (!user) { return res.status(404).json({ error: 'Email not found' }); } const resetToken = uuidv4(); const resetLink = `${process.env.FRONTEND_URL}/reset-password?token=${resetToken}`; await user.update({ reset_token: resetToken }); const languageParam = await UserParam.findOne({ where: { user_id: user.id, param_type_id: languageType.id } }); const userLanguage = languageParam ? languageParam.value : 'en'; await sendPasswordResetEmail(email, resetLink, userLanguage); res.status(200).json({ message: 'Password reset email sent' }); } catch (error) { res.status(500).json({ error: 'Error processing forgot password' }); } }; export const activateAccount = async (req, res) => { const { token } = req.body; try { const user = await User.findOne({ where: { reset_token: token } }); if (!user) { return res.status(404).json({ error: 'Invalid token' }); } await user.update({ active: true, reset_token: null }); res.status(200).json({ message: 'Account activated' }); } catch (error) { res.status(500).json({ error: 'Error activating account' }); } };