登录模块
如果登录的时候,昵称在数据库中查询的是不唯一值。需要提示用户登录失败。 即使用户密码输入正确。 然后将该消息通知相关人员,立即进行排查问题。
var express = require('express'); var router = express.Router(); // 引入连接数据库的模块 const connection=require("./connectmysql.js") /* GET users listing. */ router.get('/', function(req, res, next) { res.send('respond with a resource'); }); // 登录 router.post('/login', function (req, res) { let { username, password } = req.body // 1.构造SQL语句。 const sqlStr = `select * from account where username= '${username}' and password='${password}'` console.log('sql', username, password ) connection.query(sqlStr, (err, data) => { if (err) { throw err } else { if (!data.length) { res.send({ code: 1, msg:'请检查用户名或者密码' }); } else if(data.length==1){ // 如果查询出是2,仍然登录失败, let results = JSON.stringify(data);//把results对象转为字符串,去掉RowDataPacket let obj = JSON.parse(results);//把results字符串转为json对象 console.log(obj) res.send({ code: 0, msg: '恭喜你登录成功12', username: data.username }); } else { res.send({ code: 3, msg:'请检查用户名或者密码' }); } } }) }); module.exports = router;
前端代码
//登录 sendLogin() { axios.post('http://127.0.0.1:666/login/login', { username: '修改了', password: 'lth123456', }).then(function (response) { console.log(response); }).catch(function (error) { console.log(error); }); }
修改登录密码
修改登录密码的时候需要注意的几点: 1.在创建账户的时候,昵称就应该是唯一的。 因为如果不唯一,将无法进行修改账号。修改的时候总需要有一个唯一值进行查询到后才能够进修改。 2.修改账号的时候,如果修改的是昵称,也应该查询用户修改后的昵称是否在数据库中存在,存在的话,修改失败。
// 修改密码-->先检查原来的密码是否正确。如果正确在通过昵称进行修改密码。因为在创建的时候昵称就不可以重复 router.post('/editorpassword', function(req, res) { let { username, password, newpassword } = req.body; //1.先检查原来的密码是否正确 const sqlStr = `select * from account where username= '${username}' and password='${password}'` connection.query(sqlStr, (err, data) => { if (err) { throw err } else { if (!data.length) { res.send({ code: 1, msg:'原密码错误' }); } else if( data.length ===1){ //只有一条数据的时候才能够登录成功,否者昵称重复了。仍然登录失败 // 2.如果正确在进行修改密码。 const esitSql = `update account set password= '${newpassword}' where username= '${username}'` connection.query(esitSql, (e, d) => { if (e) { res.send({ code: 1, msg:'修改密码失败' }); throw e } else { //这个判断是否删除成功,因为有可能没有这个id的 if (d.affectedRows>0) { res.send({ code: 0, msg:'修改密码成功' }); } else { res.send({ code: 1, msg:'修改密码失败' }); } } }) } else { res.send({ code: 3, msg:'修改密码失败' }); } } }) });
前端代码
sendApi8() { axios.post('http://127.0.0.1:666/login/editorpassword', { username: '修改了', password: 'admin123', newpassword: 'admin123' }).then(function (response) { console.log(response); }).catch(function (error) { console.log(error); }); }