You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.5 KiB
JavaScript

11 months ago
const jwt = require('jsonwebtoken');
const bcrypt = require("bcrypt")
const faker = require("faker")
const User = require("../model/User")
const auth = require("../auth")
// Controller function for user registration
exports.registerUser = async (req, res) => {
11 months ago
try {
const { email, password, firstName, lastName } = req.body
// Check if the email already exists
const existingUser = await User.findOne({ email })
if (existingUser) {
return res
.status(400)
.json({ message: "This Email is already registered." })
}
// If firstName and lastName are not provided, generate default values using faker
const autoGeneratedFirstName = firstName || faker.name.firstName()
const autoGeneratedLastName = lastName || faker.name.lastName()
// Hash the password before saving it
const hashedPassword = await bcrypt.hash(password, 10)
const newUser = new User({
email,
password: hashedPassword,
firstName: autoGeneratedFirstName,
lastName: autoGeneratedLastName,
})
await newUser.save()
res.status(201).json({
message:
"User registered successfully. To update account details, acess user/update",
})
} catch (error) {
console.error(error)
res.status(500).json({ message: "Internal server error" })
}
}
// Controller function for user authentication
exports.authenticateUser = async (req, res) => {
try {
11 months ago
const { email, password } = req.body;
11 months ago
const user = await User.findOne({ email });
11 months ago
if (!user) {
return res.status(401).json({ message: 'Invalid credentials' });
}
11 months ago
const passwordMatch = await bcrypt.compare(password, user.password);
11 months ago
if (!passwordMatch) {
return res.status(401).json({ message: 'Invalid credentials' });
}
11 months ago
// Generate JWT token using the function from auth.js
const token = auth.generateToken(user._id, user.email, user.isAdmin);
// Decode JWT token to get expiration time
const decodedToken = jwt.decode(token);
if (decodedToken) {
const expiration = new Date(decodedToken.exp * 1000); // Convert seconds to milliseconds
// Log token expiration
console.log(`Login success. Token will expire on: ${expiration}`);
} else {
console.error('Error decoding token');
}
// Return user details and token
res.status(200).json({
userId: user._id,
email: user.email,
firstName: user.firstName,
lastName: user.lastName,
isAdmin: user.isAdmin,
token: token,
});
} catch (error) {
console.error(error);
res.status(500).json({ message: 'Internal server error' });
}
};
11 months ago
// Controller function for updating user data (including email, firstName, lastName, and password)
exports.updateUserData = async (req, res) => {
try {
11 months ago
const { userId, newEmail, newFirstName, newLastName, newPassword } =
req.body
const userIdFromToken = req.user.userId
if (userIdFromToken !== userId) {
return res.status(403).json({
message:
"Permission denied. You can only update your own data.",
})
}
11 months ago
const user = await User.findById(userId)
if (!user) {
11 months ago
return res.status(404).json({ message: "User not found" })
}
11 months ago
// Update email if provided
if (newEmail) {
user.email = newEmail
}
11 months ago
// Update firstName if provided
if (newFirstName) {
user.firstName = newFirstName
}
11 months ago
// Update lastName if provided
if (newLastName) {
user.lastName = newLastName
}
// Update password if provided
if (newPassword) {
const hashedPassword = await bcrypt.hash(newPassword, 10)
user.password = hashedPassword
}
// Save the updated user data
await user.save()
11 months ago
// Fetch the updated user details
const updatedUser = await User.findById(userId)
// Return the updated user details in the response
res.status(200).json({
11 months ago
message: "User data updated successfully",
user: updatedUser,
})
} catch (error) {
console.error(error)
res.status(500).json({ message: "Internal server error" })
}
}
11 months ago
exports.getUserDetails = async (req, res) => {
try {
11 months ago
const { userId } = req.params;
11 months ago
const userIdFromToken = req.user.userId;
11 months ago
if (userIdFromToken !== userId) {
return res.status(403).json({
message: "Permission denied. You can only retrieve your own data.",
});
}
const user = await User.findById(userId);
if (!user) {
11 months ago
return res.status(404).json({ message: "User not found" });
}
11 months ago
// Return the user details in the response
res.status(200).json({
user,
});
} catch (error) {
console.error(error);
11 months ago
res.status(500).json({ message: "Internal server error" });
}
};