diff --git a/backend/migrations/20240422214219-create_users_table.js b/backend/migrations/20240422214219-create_users_table.js index 97e573d1..946f6c7f 100644 --- a/backend/migrations/20240422214219-create_users_table.js +++ b/backend/migrations/20240422214219-create_users_table.js @@ -9,10 +9,13 @@ module.exports = { primaryKey: true, autoIncrement: true, }, - username: { + name: { type: Sequelize.STRING(50), allowNull: false, }, + surname: { + type: Sequelize.STRING(50), + }, email: { type: Sequelize.STRING(100), allowNull: false, diff --git a/backend/src/controllers/auth.controller.js b/backend/src/controllers/auth.controller.js index 9ab72979..515072cf 100644 --- a/backend/src/controllers/auth.controller.js +++ b/backend/src/controllers/auth.controller.js @@ -9,12 +9,12 @@ const { TOKEN_LIFESPAN } = require('../utils/constants'); const register = async (req, res) => { try { - const { username, email, password } = req.body; + const { name, surname, email, password } = req.body; const existingUser = await User.findOne({ where: { email } }); if (existingUser) return res.status(400).json({ error: "User already exists" }); const hashedPassword = await bcrypt.hash(password, 10); - const newUser = await User.create({ username, email, password: hashedPassword }); + const newUser = await User.create({ name, surname, email, password: hashedPassword }); const token = generateToken({ id: newUser.id, email: newUser.email }); await Token.create({ token, userId: newUser.id, type: 'auth' }); diff --git a/backend/src/controllers/user.controller.js b/backend/src/controllers/user.controller.js index 8796b60f..39a36755 100644 --- a/backend/src/controllers/user.controller.js +++ b/backend/src/controllers/user.controller.js @@ -11,9 +11,18 @@ const getUsersList = async (req, res) => { const { rows: users, count: totalUsers } = await User.findAndCountAll({ where: { - username: { - [Sequelize.Op.like]: `%${search}%`, - }, + [Sequelize.Op.or]: [ + { + name: { + [Sequelize.Op.like]: `%${search}%`, + }, + }, + { + surname: { + [Sequelize.Op.like]: `%${search}%`, + }, + }, + ], }, limit: parseInt(limit), offset: parseInt(offset), @@ -37,8 +46,8 @@ const getCurrentUser = async (req, res) => { const userId = req.user.id; const user = await User.findOne({ where: { id : userId } }); if (user){ - const { username, email, role } = user; - return res.status(200).json({ user: { username, email, role } }); + const { name, surname, email, role } = user; + return res.status(200).json({ user: { name, surname, email, role } }); } else{ return res.status(400).json({ error: "User not found" }); diff --git a/backend/src/models/User.js b/backend/src/models/User.js index 122997a0..eb094cf8 100644 --- a/backend/src/models/User.js +++ b/backend/src/models/User.js @@ -7,10 +7,13 @@ module.exports = (sequelize, DataTypes) => { primaryKey: true, autoIncrement: true, }, - username: { + name: { type: DataTypes.STRING(50), allowNull: false, }, + surname: { + type: DataTypes.STRING(50), + }, email: { type: DataTypes.STRING(100), allowNull: false, diff --git a/frontend/src/scenes/login/CreateAccountPage.jsx b/frontend/src/scenes/login/CreateAccountPage.jsx index 8c76cbbd..f9993343 100644 --- a/frontend/src/scenes/login/CreateAccountPage.jsx +++ b/frontend/src/scenes/login/CreateAccountPage.jsx @@ -47,15 +47,14 @@ function CreateAccountPage() { const handleSignUp = async () => { const { name, surname, email, password } = formData; - const { isUsernameValid, isEmailValid, isPasswordValid } = validation; + const { isNameValid, isSurnameValid, isEmailValid, isPasswordValid } = validation; - if (!isUsernameValid || (surname && !isSurnameValid) || !isEmailValid || !isPasswordValid) { + if (!isNameValid || (surname && !isSurnameValid) || !isEmailValid || !isPasswordValid) { alert('Please fill out the form correctly.'); return; } - const username = surname ? (name + " " + surname) : name; - const userData = { username: username, email: email, password: password }; + const userData = { name: name, surname: surname, email: email, password: password }; try { const response = await signUp(userData);