Are you really clear about node registration and login?

1. Create a data table
1.1 Create user table

Insert image description here

2Install and configure the mysql file
2.1 Install the mysql module
npm i mysql@2.18.1
2.2 In db index
const mysql=require('mysql')

//创建数据库连接对象

const db=mysql.createPool({
    
    
    host:'localhost',
    user:'root',
    password:'123456',
    database:'my_sql-o1'
})

//向外共享
module.exports=db
3.Registration function

General process of registration
1. Verify whether the form data is legal
2. Check whether the user name is occupied
3. Password encryption processing
4. Insert new user

3.1 Check that the form data is legal
 //对客户端的数据进行校验
  if(userInfo.username==''||userInfo.password==''){
    
    
   return res.send({
    
    
        status:1,
        msg:'用户名和密码不能为空'
    })
  }
3.2 Determine user occupancy
 //查询用户是否重复
  let sql='select * from ev_users where username=?'
  db.query(sql,[userInfo.username],(error,result)=>{
    
    
    if(error) {
    
    
        return res.send({
    
    status:1,msg:error.message})
    }
    console.log(result.length)
    if(result.length>0){
    
    
        return res.send({
    
    status:1,msg:"用户被占用"})
    }
3.3 Password encryption

1. Download bcryptjs

npm i bcryptjs@2.4.3

2.Introduce bcryptjs


//引入加密包
const bcrypt=require('bcryptjs')

 //密码加密
   userInfo.password=bcrypt.hashSync(userInfo.password,10)
3.4 Insert user
 let sql1='insert into ev_users set ?'
   db.query(sql1,{
    
    username:userInfo.username,password:userInfo.password},(error,result)=>{
    
    
    if(error) return res.send({
    
    
        status:1,
        msg:error.message
    })
    res.send({
    
    
        status:0,
        msg:"插入成功"
    })
   })
Encapsulate error handling function

in app.js

//封装错误处理函数
app.use((req,res,next)=>{
    
    
    res.cc=function(err,status=1){
    
    
        res.send({
    
    
            status,
            message:err instanceof Error ?err.message:err
        })
    }
    next()
})

Called in code

 return res.cc('用户被占用')

Insert image description here

4. Login function

General login process
1. Determine whether the back-end data submitted by the front-end is legal.
2. Check whether the logged in user exists.
3. Determine whether the current user's password is correct.

4.1 Judgment of login password

bcrypt.compareSync determines whether the password and data password entered by the user are correct, and its return type is a Boolean value.

//检验密码是否正确
 const compareResult=bcrypt.compareSync(userInfo.password,result[0].password)
4.2 Generate token characters

1. Download jsonwebtoken

npm i jsonwebtoken@8.5.1

2.Import

//引入token
const jwt=require('jsonwebtoken')

3. Configure encryption form
Insert image description here
4. Token encryption

jwt.sign has three parameters, which are the data to generate the token, the encrypted form, and the validity period of the token.

//将用户的信息加密,生成token
         const tokenStr=jwt.sign(user,config.jwtscrestKey,{
    
    
            expiresIn:'10h'
         })
res.send({
    
    
            status:0,
            msg:'登录成功',
            token:'Bearer '+tokenStr
         })
4.3 Parsing token middleware

1. Install parsing middleware

npm i express-jwt@5.3.3

2.Introduced in App.js

//token解析中间件
const expressJWT=require('express-jwt')
//映入解密
const config=require('./config')

app.use(expressJWT({
    
    secret:config.jwtscrestKey}).unless({
    
    path:['/^\/api/']}))

3. In the error middleware

//错误中间件
app.use((req,res,err,next)=>{
    
    
    if(err.name=='UnauthorizedError') return res.cc('身份认证失败')
     res.cc(err)
})

Guess you like

Origin blog.csdn.net/qq_48164590/article/details/130403714