这篇“vue登录注册sql语句如何写”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“vue登录注册sql语句如何写”文章吧。
注册
app.post('/register',function(req,res){
const {username, password} = req.body
const sql = 'insert into users (username,password) values (?,?)'
connection.query(sql,[username,password],(err,result)=>{
if(err){
console.log(err)
res.send({
code:1,
msg:'用户名已存在!'
})
}else{
res.send({
code:0,
msg:'注册成功!'
})
}
})})
登录
app.post('/login',function(req,res){
const {username, password} = req.body
const sql = 'select * from users where username=? and password=?'
connection.query(sql,[username,password],(err,result)=>{
if(err){
console.log(err)
}
if(result.length===0){
res.send({
code:1,
msg:'用户名或密码错误!'
})
}else{
const userInfo = JSON.stringify({
id:result[0].id,
username:result[0].username
})
res.send({
code:0,
msg:'登录成功',
userInfo
})
}
})})
在上面的代码中,我们通过使用express框架定义两个路由:一个用于注册,另一个用于登录。在post请求中,我们从req.body中获取用户名和密码,并将它们发送到数据库中进行验证。如果验证成功,我们将用户信息存储在userInfo对象中,并将其发送回前端。
需要注意的是,在使用Vue.js时,我们通常会将用户信息存储在浏览器的本地存储中,并在以后的会话中使用。下面是一个简单的示例,演示如何将用户信息存储在localStorage中:
存储用户信息
localStorage.setItem('userInfo', JSON.stringify(userInfo))
获取用户信息
localStorage.getItem('userInfo')