我尝试使用 const bcrypt = require('bcrypt'); 在 Node js 中加密密码但它不是散列。当您在终端上尝试其哈希值但不在数据库上尝试时,只需发布在输入上键入的纯文本。
安装和导入
const bcrypt = require('bcrypt');
npm 安装 bcrypt@latest
// Sign-up route
app.post('/signup', async (req, res) => {
const { fullName, email, password } = req.body;
try {
// Hash the password before storing it in the database
const hashedPassword = await bcrypt.hash(password, 10);
// Insert user data into the database
const newUser = { fullName, email, password: hashedPassword };
db.query('INSERT INTO users SET ?', newUser, (err) => {
if (err) {
console.error('Error inserting user:', err);
res.status(500).json({ error: 'Internal server error' });
return;
}
res.json({ message: 'User registered successfully' });
});
} catch (error) {
console.error('Error hashing password:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
我希望密码经过哈希处理,例如密码:123,这个
$2b$10$8hL4zs.XUIXpc6sxf/kfYeKWAcwduZaMlUsZoRz8.cRc5kz9EzEV2
应该发布到数据库数据类型密码
希望这对您有帮助。
const bcrypt = require('bcrypt');
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydatabase',
});
connection.connect();
app.post('/signup', async (req, res) => {
const { fullName, email, password } = req.body;
try {
// Hash the password before storing it in the database
const hashedPassword = await bcrypt.hash(password, 10);
// Insert user data into the database
const newUser = { fullName, email, password: hashedPassword };
connection.query('INSERT INTO users SET ?', newUser, (err) => {
if (err) {
console.error('Error inserting user:', err);
res.status(500).json({ error: 'Internal server error' });
return;
}
res.json({ message: 'User registered successfully' });
});
} catch (error) {
console.error('Error hashing password:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
connection.end();
const bcrypt = require('bcrypt');
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: 'password',
database: 'mydatabase',
});
connection.connect();
app.post('/signup', async (req, res) => {
const { fullName, email, password } = req.body;
try {
// Hash the password before storing it in the database
const hashedPassword = await bcrypt.hash(password, 10);
// Insert user data into the database
const newUser = { fullName, email, password: hashedPassword };
connection.query('INSERT INTO users SET ?', newUser, (err) => {
if (err) {
console.error('Error inserting user:', err);
res.status(500).json({ error: 'Internal server error' });
return;
}
res.json({ message: 'User registered successfully' });
});
} catch (error) {
console.error('Error hashing password:', error);
res.status(500).json({ error: 'Internal server error' });
}
});
connection.end();