Spaces:
Sleeping
Sleeping
| const User = require('../models/User'); | |
| const jwt = require('jsonwebtoken'); | |
| const register = async (req, res) => { | |
| try { | |
| const { email, password, roles, role, firstName, lastName } = req.body; | |
| // Use `roles` array if provided, fall back to single `role` | |
| const selectedRoles = roles && roles.length > 0 ? roles : [role || 'customer']; | |
| const primaryRole = selectedRoles[0]; | |
| const userExists = await User.findOne({ email }); | |
| if (userExists) { | |
| // If user exists, try to merge roles (add new roles to existing ones) | |
| const isMatch = await userExists.comparePassword(password); | |
| if (isMatch) { | |
| // Merge new roles with existing ones | |
| const mergedRoles = [...new Set([...userExists.roles, ...selectedRoles])]; | |
| userExists.roles = mergedRoles; | |
| userExists.role = mergedRoles[0]; // keep first as primary | |
| await userExists.save(); | |
| return res.status(200).json({ | |
| _id: userExists._id, | |
| email: userExists.email, | |
| role: userExists.role, | |
| roles: userExists.roles, | |
| firstName: userExists.firstName, | |
| lastName: userExists.lastName, | |
| token: generateToken(userExists._id), | |
| }); | |
| } | |
| return res.status(400).json({ message: 'User already exists with this email.' }); | |
| } | |
| const user = await User.create({ | |
| email, password, | |
| role: primaryRole, | |
| roles: selectedRoles, | |
| firstName, lastName | |
| }); | |
| if (user) { | |
| res.status(201).json({ | |
| _id: user._id, | |
| email: user.email, | |
| role: user.role, | |
| roles: user.roles, | |
| firstName: user.firstName, | |
| lastName: user.lastName, | |
| token: generateToken(user._id) | |
| }); | |
| } | |
| } catch (error) { | |
| res.status(500).json({ message: error.message }); | |
| } | |
| }; | |
| const login = async (req, res) => { | |
| try { | |
| const { email, password } = req.body; | |
| console.log(`--- Login Attempt ---`); | |
| console.log(`Input Email: "${email}"`); | |
| const user = await User.findOne({ email: email.trim() }); | |
| if (!user) { | |
| console.log(`User NOT found for email: "${email}"`); | |
| return res.status(401).json({ message: 'No account found with this email address.' }); | |
| } | |
| console.log(`User found: ${user.email}`); | |
| const isMatch = await user.comparePassword(password); | |
| console.log(`Password match result: ${isMatch}`); | |
| if (isMatch) { | |
| res.json({ | |
| _id: user._id, | |
| email: user.email, | |
| role: user.role, | |
| roles: user.roles && user.roles.length > 0 ? user.roles : [user.role], | |
| firstName: user.firstName, | |
| lastName: user.lastName, | |
| profilePicture: user.profilePicture, | |
| bio: user.bio, | |
| specialty: user.specialty, | |
| price: user.price, | |
| address: user.address, | |
| experience: user.experience, | |
| rating: user.rating, | |
| isVerified: user.isVerified, | |
| portfolio: user.portfolio, | |
| token: generateToken(user._id) | |
| }); | |
| } else { | |
| console.log(`Password comparison failed for ${email}`); | |
| res.status(401).json({ message: 'Incorrect password. Please try again.' }); | |
| } | |
| } catch (error) { | |
| console.error(`Login error:`, error); | |
| res.status(500).json({ message: error.message }); | |
| } | |
| }; | |
| const generateToken = (id) => { | |
| return jwt.sign({ id }, process.env.JWT_SECRET || 'fallback_demo_secret_key_123', { | |
| expiresIn: '30d' | |
| }); | |
| }; | |
| module.exports = { register, login }; | |