All files / server/routes register.js

100% Statements 13/13
100% Branches 4/4
100% Functions 1/1
100% Lines 12/12
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 214x 4x   4x 3x 3x 2x 2x 1x   1x       1x 1x       4x  
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