POS_node/controllers/auth.js

137 lines
3.9 KiB
JavaScript
Raw Normal View History

2024-08-27 08:37:01 +08:00
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 {
2024-08-30 16:35:49 +08:00
// fName,
// lName,
2024-08-27 08:37:01 +08:00
email,
password,
2024-08-30 16:35:49 +08:00
// phone,
// address1,
// address2,
// city,
// province,
// country,
// zip,
// username,
// type,
// status,
// transactions,
2024-08-27 08:37:01 +08:00
} = 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,
});
2024-08-30 16:35:49 +08:00
// const result = await User.dropIndex('username_1');
// const result = await User.collection.dropIndex('username_1');
2024-08-27 08:37:01 +08:00
const salt = await bcrypt.genSalt();
const passwordHash = await bcrypt.hash(password, salt);
const emailTaken = await User.findOne({ email: email });
2024-08-30 16:35:49 +08:00
// const usernameTaken = await User.findOne({ username: username });
2024-08-27 08:37:01 +08:00
// 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" }],
});
}
2024-08-30 16:35:49 +08:00
if (!emailTaken) {
2024-08-27 08:37:01 +08:00
const newUser = new User({
2024-08-30 16:35:49 +08:00
// fName,
// lName,
2024-08-27 08:37:01 +08:00
email,
password: passwordHash,
2024-08-30 16:35:49 +08:00
// phone,
// address1,
// address2,
// city,
// province,
// country,
// zip,
// username,
// type,
// status,
// transactions,
// photo: pic ? pic : "",
2024-08-27 08:37:01 +08:00
});
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 },
2024-08-30 16:35:49 +08:00
// { username: users },
2024-08-27 08:37:01 +08:00
]);
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 });
}
};