Create a user.js file in the service, add the query information and insert the code

An empty object was returned when the queryUserName method was called, ok

Insert table jacktest.user = jacktest.user = jacktest.user = jacktest.user = jacktest.user = jacktest.user = jacktest.user = jacktest.user = jacktest.user = jacktest.user

create table users(
  user_id int(10) unsigned NOT NULL auto_increment,
  username varchar(30),
  password varchar(30),
  created_at timestamp.primary key (user_id)
)engine=InnoDB AUTO_INCREMENT=1 comment 'User table'
Copy the code

Now we run the code again. Success. Then make a simple change to the code, and the log-in logic is complete

// Logon logic
  async login() {
    const body = this.ctx.request.body;
    const { username, password } = body;
    const { user } = this.ctx.service;
    let postBody = {
      code: 0.msg: ' '.data: {}};// Check whether it already exists
    const userData = await user.queryUserName(username);
    if (userData && Object.keys(userData).length) { / / has been in existence
      if(password === userData.password) { postBody = { ... postBody,code: 1.msg: 'Login successful'.data: { userData },
        };
        this.ctx.cookies.set('token', userData.user_id);
      } else {
        postBody.msg = 'Wrong account or password';
        postBody.code = 0;
      }
      // Check whether the password is correct
      this.ctx.body = postBody;
      return;
    }
    this.ctx.body = { ... postBody,msg: 'User does not exist'}; }// Register logic
  async register() {
    const body = this.ctx.request.body;
    const { username, password } = body;
    const { user } = this.ctx.service;
    const postBody = {
      code: 0.msg: ' '.data: {}};// Check whether it already exists
    const userIsExists = await user.queryUserName(username);
    if (userIsExists && Object.keys(userIsExists).length) { / / has been in existence
      this.ctx.body = { ... postBody,msg: 'User already exists'};return;
    }
    // Create a user
    const newUser = await user.insertUser({ username, password });
    if (newUser.affectedRows === 1) {
      const userData = await user.queryUserName(username);
      postBody.data = {
        userData,
      };
    }

    this.ctx.body = { ... postBody,msg: 'Registration successful'}; }Copy the code