-
Anonymous
Почему моя регистрация по -прежнему не удалась после операции `users.insertone ()` буферизация, пройденная через 10000 м
Сообщение
Anonymous »
Введите описание изображения здесь < /p>
Код: Выделить всё
authController.js under backend> controllers file
const User = require('../models/User');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
exports.register = async (req,res) => {
try {
const {username, email, mobile, password, role} = req.body;
// Hash password
const salt = await bcrypt.genSalt(10);
const hashed = await bcrypt.hash(password, salt);
// Save user
const user = new User({username, email, mobile, password: hashed, role});
await user.save();
res.status(201).json({message: "User registered"});
} catch (err) {
res.status(500).json({error:err.message});
}
};
exports.login = async (req, res) => {
try {
const {email, password} = req.body;
const user = await User.findOne({ email });
if (!user) return res.status(404).json({ message: "User not found" });
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) return res.status(401).json({ message: "Wrong password" });
const token = jwt.sign({id: user._id, role: user.role }, process.env.JWT_SECRET, { expiresIn: '2d'});
res.status(200).json({token, user: { id: user._id, username: user.username, role: user.role } });
} catch (err) {
res.status(500).json({ rror:err.message});
}
};
pls Помощь с моим кодом контроллера Auth, если это возможно, и предлагаемым сайтом кода Frontend для моего регистрации.>
Подробнее здесь:
https://stackoverflow.com/questions/796 ... e-bufferin
1748485638
Anonymous
Введите описание изображения здесь < /p>
[code]authController.js under backend> controllers file
const User = require('../models/User');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
exports.register = async (req,res) => {
try {
const {username, email, mobile, password, role} = req.body;
// Hash password
const salt = await bcrypt.genSalt(10);
const hashed = await bcrypt.hash(password, salt);
// Save user
const user = new User({username, email, mobile, password: hashed, role});
await user.save();
res.status(201).json({message: "User registered"});
} catch (err) {
res.status(500).json({error:err.message});
}
};
exports.login = async (req, res) => {
try {
const {email, password} = req.body;
const user = await User.findOne({ email });
if (!user) return res.status(404).json({ message: "User not found" });
const isMatch = await bcrypt.compare(password, user.password);
if (!isMatch) return res.status(401).json({ message: "Wrong password" });
const token = jwt.sign({id: user._id, role: user.role }, process.env.JWT_SECRET, { expiresIn: '2d'});
res.status(200).json({token, user: { id: user._id, username: user.username, role: user.role } });
} catch (err) {
res.status(500).json({ rror:err.message});
}
};
[/code]
pls Помощь с моим кодом контроллера Auth, если это возможно, и предлагаемым сайтом кода Frontend для моего регистрации.>
Подробнее здесь: [url]https://stackoverflow.com/questions/79643213/why-is-my-registration-still-failed-after-operation-users-insertone-bufferin[/url]