137 lines
3.9 KiB
JavaScript
137 lines
3.9 KiB
JavaScript
import bcrypt from "bcrypt";
|
|
import jwt from "jsonwebtoken";
|
|
import User from "../models/User.js";
|
|
import cloudinary from "cloudinary";
|
|
import streamifier from "streamifier";
|
|
import DatauriParser from "datauri/parser.js";
|
|
import path from "path";
|
|
|
|
const parser = new DatauriParser();
|
|
|
|
/* REGISTER USER */
|
|
export const register = async (req, res) => {
|
|
try {
|
|
const {
|
|
// fName,
|
|
// lName,
|
|
email,
|
|
password,
|
|
// phone,
|
|
// address1,
|
|
// address2,
|
|
// city,
|
|
// province,
|
|
// country,
|
|
// zip,
|
|
// username,
|
|
// type,
|
|
// status,
|
|
// transactions,
|
|
} = req.body;
|
|
cloudinary.config({
|
|
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
|
|
api_key: process.env.CLOUDINARY_API_KEY,
|
|
api_secret: process.env.CLOUDINARY_API_SECRET,
|
|
});
|
|
// const result = await User.dropIndex('username_1');
|
|
// const result = await User.collection.dropIndex('username_1');
|
|
const salt = await bcrypt.genSalt();
|
|
const passwordHash = await bcrypt.hash(password, salt);
|
|
const emailTaken = await User.findOne({ email: email });
|
|
// const usernameTaken = await User.findOne({ username: username });
|
|
|
|
// const phoneTaken = await User.findOne({ phone: phone });
|
|
const pic = null;
|
|
if (req.file) {
|
|
const extName = path.extname(req.file.originalname).toString();
|
|
const file64 = parser.format(extName, req.file.buffer);
|
|
pic = await cloudinary.uploader.upload(file64.content, {
|
|
folder: "obPayUserPhoto",
|
|
transformation: [{ width: 500, height: 500, crop: "limit" }],
|
|
});
|
|
}
|
|
|
|
if (!emailTaken) {
|
|
const newUser = new User({
|
|
// fName,
|
|
// lName,
|
|
email,
|
|
password: passwordHash,
|
|
// phone,
|
|
// address1,
|
|
// address2,
|
|
// city,
|
|
// province,
|
|
// country,
|
|
// zip,
|
|
// username,
|
|
// type,
|
|
// status,
|
|
// transactions,
|
|
// photo: pic ? pic : "",
|
|
});
|
|
const usersaved = await newUser.save();
|
|
const user = await User.findOne({ email: email });
|
|
|
|
res.status(201).json({ usersaved, user });
|
|
} else if (emailTaken) {
|
|
res.status(400).json({ error: "email taken" });
|
|
} else if (usernameTaken) {
|
|
res.status(404).json({ error: "username taken" });
|
|
}
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
console.log(err.message);
|
|
}
|
|
};
|
|
|
|
/* LOGGING IN */
|
|
// export const login = async (req, res) => {
|
|
// try {
|
|
// const { users, password } = req.body;
|
|
|
|
// // const user = await User.findOne({ email: users });
|
|
// const user = await User.findOne().or([{ email: users }, { phone: users }]);
|
|
// if (user) {
|
|
// const isMatch = await bcrypt.compare(password, user.password);
|
|
// if (isMatch) {
|
|
// const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET);
|
|
// delete user.password;
|
|
// res.status(200).json({token, user});
|
|
// } else {
|
|
// return res.status(400).json({ msg: "Invalid credentials. " });
|
|
// }
|
|
// } else if (!user)
|
|
// console.log(users,password)
|
|
// return res.status(404).json({ msg: "User does not exist. " });
|
|
|
|
// } catch (err) {
|
|
// res.status(500).json({ error: err.message });
|
|
|
|
export const login = async (req, res) => {
|
|
try {
|
|
const { users, password } = req.body;
|
|
|
|
const user = await User.findOne().or([
|
|
{ email: users },
|
|
{ phone: users },
|
|
// { username: users },
|
|
]);
|
|
if (!user) {
|
|
console.log("does not exist");
|
|
return res.status(404).json({ msg: "User does not exist. " });
|
|
}
|
|
|
|
const isMatch = await bcrypt.compare(password, user.password);
|
|
if (!isMatch) {
|
|
return res.status(400).json({ msg: "Invalid credentials. " });
|
|
}
|
|
|
|
const token = jwt.sign({ id: user._id }, process.env.JWT_SECRET);
|
|
delete user.password;
|
|
res.status(200).json({ token, user });
|
|
} catch (err) {
|
|
res.status(500).json({ error: err.message });
|
|
}
|
|
};
|