added login, first preparation for menu
This commit is contained in:
@@ -1,40 +1,11 @@
|
||||
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;
|
||||
import * as userService from '../services/authService.js';
|
||||
|
||||
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 });
|
||||
const result = await userService.registerUser({ email, username, password, language });
|
||||
res.status(201).json(result);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
res.status(500).json({ error: error.message });
|
||||
@@ -42,56 +13,33 @@ export const register = async (req, res) => {
|
||||
};
|
||||
|
||||
export const login = async (req, res) => {
|
||||
const { email, password } = req.body;
|
||||
const { username, 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 });
|
||||
const result = await userService.loginUser({ username, password });
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Error logging in' });
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
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' });
|
||||
const result = await userService.handleForgotPassword({ email });
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Error processing forgot password' });
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
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' });
|
||||
const result = await userService.activateUserAccount({ token });
|
||||
res.status(200).json(result);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: 'Error activating account' });
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user