estatesfunding/ef-api/controllers/user.js

131 lines
3.5 KiB
JavaScript
Raw Normal View History

2024-09-04 00:52:26 +00:00
import dotenv from "dotenv";
import UserModal from "../models/user.js";
import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken";
2024-09-07 14:08:04 +00:00
import { sendEmail } from "../utils/sendEmail.js";
2024-09-04 00:52:26 +00:00
dotenv.config();
const secret = process.env.SECRET_KEY;
// This is signup
export const signup = async (req, res) => {
2024-09-07 11:26:52 +00:00
const { title, email, password, firstName, middleName, lastName, termsconditions, userType } = req.body;
2024-09-04 00:52:26 +00:00
try {
// Check if user already exists
const oldUser = await UserModal.findOne({ email });
if (oldUser) {
return res.status(400).json({ message: "User already exists" });
}
// Hash the password
const hashedPassword = await bcrypt.hash(password, 12);
// Create new user
const result = await UserModal.create({
2024-09-07 07:48:55 +00:00
title,
2024-09-04 00:52:26 +00:00
email,
password: hashedPassword,
firstName,
2024-09-07 05:39:50 +00:00
middleName,
2024-09-04 00:52:26 +00:00
lastName,
2024-09-07 07:48:55 +00:00
termsconditions,
2024-09-07 11:26:52 +00:00
userType,
2024-09-04 00:52:26 +00:00
});
// Generate JWT (if needed)
const token = jwt.sign({ email: result.email, id: result._id }, secret, { expiresIn: "7h" });
// Save the token in the user's tokens array
result.tokens.push({ token });
await result.save();
2024-09-07 13:58:47 +00:00
const url = `Click on the link ${process.env.RETURN_URL}/users/${result._id}/verify/${token}`;
await sendEmail(result.email, "Verify Email", url);
2024-09-07 14:08:04 +00:00
console.log("url", url);
2024-09-07 13:58:47 +00:00
2024-09-04 00:52:26 +00:00
// Send the user info and token back to the client
res.status(201).json({ result, token });
} catch (error) {
console.error(error);
res.status(500).json({ message: "Something went wrong" });
}
};
2024-09-07 13:58:47 +00:00
//This is to verify user
export const verifyUser = async (req, res) => {
try {
const user = await UserModal.findOne({ _id: req.params.id });
if (!user) return res.status(400).send({ message: "Invalid link" });
const token = user.tokens.find(
(tokenObj) => tokenObj.token === req.params.token
);
if (!token) return res.status(400).send({ message: "Invalid link" });
// Update the user's verified status to true
user.verified = true;
await user.save();
// Remove the token from the tokens array
user.tokens = user.tokens.filter(
(tokenObj) => tokenObj.token !== req.params.token
);
await user.save();
res.status(200).send({ message: "Email verified successfully" });
} catch (error) {
res.status(500).send({ message: "Internal Server Errors" });
}
};
2024-09-07 07:48:55 +00:00
// This is signIn
2024-09-04 00:52:26 +00:00
export const signin = async (req, res) => {
const { email, password } = req.body;
try {
const oldUser = await UserModal.findOne({ email });
if (!oldUser)
return res.status(404).json({ message: "User doesn't exist" });
const isPasswordCorrect = await bcrypt.compare(password, oldUser.password);
if (!isPasswordCorrect)
return res.status(400).json({ message: "Invalid credentials" });
// if (!oldUser.verified) {
// return res.status(401).json({ message: "User is not verified" });
// }
const token = jwt.sign({ email: oldUser.email, id: oldUser._id }, secret, {
expiresIn: "8h",
});
res.status(200).json({ result: oldUser, token });
} catch (error) {
res.status(500).json({ message: "Something went wrong" });
console.log(error);
}
2024-09-06 15:02:20 +00:00
};
//To show user
export const showUser = async (req, res) => {
const { id } = req.params;
try {
const user = await UserModal.findById({id});
if (!user) {
return res.status(404).json({ message: "User not found" });
}
res.status(200).json(user);
} catch (error) {
console.error(error);
res.status(500).json({ message: "Internal servers error" });
}
};