demand

  • The new staff
    • The request header requires a token
    • Nickname was added. The nickname was not the same as the nickname
    • Password The default password is 123456 md5 ciphertext
  • Change the password
    • The request header requires a token
    • Parameter transfer: New password

implementation

The code is basically unchanged, only routing (router.js), controller (Controller) and Service (Service) need to be changed.

  • The following items are similar to the previous article (Egg login authentication (3) : MD5 encryption and authentication of passwordThe same)
    • The database
    • Depend on the package
    • config/config.default.js
    • config/plugin.js
    • app/model/user.js

app/controller/user.js

'use strict';

const Controller = require('egg').Controller;

class UserController extends Controller {
  / / login
  async login() {
    const { ctx, app } = this;
    const data = ctx.request.body;
    // Check whether the user exists and the password is correct
    const isValidUser = await ctx.service.user.validUser(data.nickname, data.password);
    if (isValidUser) {
      const token = app.jwt.sign({ nickname: data.nickname }, app.config.jwt.secret);
      ctx.body = { code: 200.msg: 'Login successful', token };
    } else {
      ctx.body = { code: 400.msg: 'Login failed'}; }}// Get all users
  async index() {
    const { ctx } = this;
    const data = await ctx.service.user.getUser();
    ctx.body = { code: 200.msg: 'Query successful', data };
  }
  // Get the user by id
  async show() {
    const { ctx } = this;
    const data = await ctx.service.user.getUser(ctx.params.id);
    ctx.body = { code: 200.msg: 'Query successful', data };
  }
  // Create a person
  async create() {
    const { ctx } = this;
    const { nickname } = ctx.request.body;
    await ctx.service.user.addUser(nickname);
    ctx.body = { code: 200.msg: 'Added successfully' };
  }
  // Change the password
  async updatePwd() {
    const { ctx } = this;
    const { password } = ctx.request.body;
    await ctx.service.user.editPwd(ctx.params.id, password);
    ctx.body = { code: 200.msg: 'Modified successfully'}; }}module.exports = UserController;
Copy the code

app/service/user.js

'use strict';

const Service = require('egg').Service;
const crypto = require('crypto');
// Set the default password
const DEFAULT_PWD = '123456';
function toInt(str) {
  if (typeof str === 'number') return str;
  if(! str)return str;
  return parseInt(str, 10) | |0;
}

class UserService extends Service {
  // query the user table and verify the password and flower name
  async validUser(nickname, password) {
    const data = await this.getUser();
    const pwd = this.getMd5Data(password);
    for (const item of data) {
      if (item.nickname === nickname && item.password === pwd) return true;
    }
    return false;
  }
  // Get the user id
  async getUser(id) {
    const { ctx } = this;
    const query = { limit: toInt(ctx.query.limit), offset: toInt(ctx.query.offset) };
    if (id) {
      return await ctx.model.User.findByPk(toInt(id));
    }
    return await ctx.model.User.findAll(query);
  }
  // Add new staff
  async addUser(nickname) {
    const { ctx } = this;
    const password = this.getMd5Data(DEFAULT_PWD);
    await ctx.model.User.create({ nickname, password });
  }
  // Change the password
  async editPwd(id, password) {
    const { ctx } = this;
    const user = await ctx.model.User.findByPk(id);
    const newPwd = this.getMd5Data(password);
    await user.update({ password: newPwd });
  }
  // Md5 encryption is used to encrypt data. If you enter the plain text, ciphertext is returned
  getMd5Data(data) {
    return crypto.createHash('md5').update(data).digest('hex'); }}module.exports = UserService;
Copy the code

app/router.js

'use strict';

/ * * *@param {Egg.Application} app - egg application
 */
module.exports = app= > {
  const { router, controller, jwt } = app;
  router.get('/', controller.home.index);

  router.post('/user/login', controller.user.login);
  / / query
  router.get('/user', controller.user.index);
  router.get('/user/:id', jwt, controller.user.show);
  / / new
  router.put('/user', jwt, controller.user.create);
  // Change the password
  router.post('/user/:id', jwt, controller.user.updatePwd);
};
Copy the code

self-test

  • The new staff

  • Change the password