estatesfunding/ef-api/controllers/user.js

130 lines
3.5 KiB
JavaScript

import dotenv from "dotenv";
import UserModal from "../models/user.js";
import bcrypt from "bcryptjs";
import jwt from "jsonwebtoken";
import { sendEmail } from "../utils/sendEmail.js";
dotenv.config();
const secret = process.env.SECRET_KEY;
// This is signup
export const signup = async (req, res) => {
const { title, email, password, firstName, middleName, lastName, termsconditions, userType } = req.body;
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({
title,
email,
password: hashedPassword,
firstName,
middleName,
lastName,
termsconditions,
userType,
});
// 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();
const url = `Click on the link ${process.env.RETURN_URL}/users/${result._id}/verify/${token}`;
await sendEmail(result.email, "Verify Email", url);
// 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" });
}
};
//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" });
}
};
// This is signIn
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);
}
};
//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" });
}
};