我正在使用猫鼬,/login
是发出发布请求以检查详细信息并登录用户的途径。当我在用户界面中实现它时,似乎接受了正确的输入登录信息即使它们绝对不是。谁能看到我要去哪里错了?
我基于以下视频创建了登录和注册系统:https://www.youtube.com/watch?v=yJchGDVZFTA
用于处理帖子请求的代码
users.post('/login', (req, res) => {
User.findOne({
email: req.body.email
})
.exec()
.then(user => {
if (user) {
console.log('found user')
if (bcrypt.compareSync(req.body.password, user.password)) {
console.log('password correct')
console.log(bcrypt.compareSync(req.body.password, user.password))
const payload = {
_id: user._id,
email: user.email,
f_name: user.f_name,
s_name: user.s_name
}
console.log(payload)
let token = jwt.sign(payload, process.env.SECRET_KEY, {
expiresIn: '36000'
})
res.send(token)
} else {
res.json({
error: 'Password Incorrect'
})
}
} else {
res.json({
error: 'No user with that email address was found'
})
}
})
.catch(err => {
res.send('error: ' + err)
})
})
用户Vue组件中用于处理用户输入数据并发出发布请求的代码
<script>
import axios from 'axios'
import router from '../main.js'
import EventBus from './EventBus'
export default {
data () {
return {
email: '',
password: ''
}
},
methods: {
login () {
axios
.post('users/login', {
email: this.email,
password: this.password
})
.then(res => {
localStorage.setItem('usertoken', res.data)
this.email = ''
this.password = ''
console.log(this.email + this.password)
router.push({
name: 'dashboard'
})
this.emitMethod()
})
.catch(err => {
console.log(err)
})
},
emitMethod () {
EventBus.$emit('logged-in', true)
}
}
}
</script>
编辑: 如果我使用Postman并向登录路径发出发布请求,则可以正常工作并在电子邮件和密码错误时返回错误/在电子邮件和密码正确时返回令牌。