All files / vue-koa-demo/server/routes register.js

0% Statements 0/13
0% Branches 0/4
0% Functions 0/1
0% Lines 0/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21                                         
const User = require('../models/user')
const bcrypt = require('bcryptjs')
 
const register = async (ctx, next) => {
  const { name, pwd } = ctx.request.body
  if (!name) throw new Error('name is required')
  const isExist = await User.findOne({ userId: name })
  if (isExist) {
    ctx.body = { success: false }
  } else {
    const userDoc = await User.create({
      userId: name,
      userPwd: bcrypt.hashSync(pwd)
    })
    await userDoc.save()
    ctx.body = { success: true }
  }
}
 
module.exports = register