【发布时间】:2018-06-21 18:39:56
【问题描述】:
我有一个用于登录单个密码 Web 应用程序的反应组件。我在输入上有一个 onChange={this.setState({password: event.target.value}} 道具来更新密码。然后当用户点击提交按钮时,它会调用这个函数:
onSubmitSignIn = () => {
fetch('http://localhost:3000/signin', {
method: 'post',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
password: this.state.password
})
})
.then(response => response.json())
.then(response => {
if (response === 'true') {
this.props.onRouteChange('home');
}
})
}
这是我的服务器的样子:
const express = require('express')
const app = express()
const bcrypt = require('bcrypt-nodejs')
var bodyParser = require('body-parser')
var cors = require('cors')
app.use(cors());
app.use(bodyParser.json());
app.get('/', (req, res) => res.send('Hello World!'))
bcrypt.hash("password", null, null, function(err, hash) {
console.log(hash)
});
app.post('/signin', (req, res) => {
var a = JSON.parse(req.body);
bcrypt.compare(a.password, hashNew, function(err, res) {
if (hash === hashNew) {
res.send(true);
} else {
res.send(false);
}
});
})
app.listen(3000, () => console.log('App listening on port 3000!'))
我正在尝试使用 bcrypt 作为全局变量来生成哈希值,以与为用户在提交时输入的密码创建的哈希值进行比较。感谢您的任何帮助。如果有什么需要澄清的,请告诉我。
【问题讨论】: