register.js 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
const {
  userCollection,
  LOG_TYPE
} = require('../../common/constants')
const {
  ERROR
} = require('../../common/error')
const {
  findUser
} = require('./account')
const {
  getValidInviteCode,
  generateInviteInfo
} = require('./fission')
DCloud_JSON's avatar
DCloud_JSON 已提交
15 16 17
const {
  logout
} = require('./logout')
18 19 20
const PasswordUtils = require('./password')
const merge = require('lodash.merge')

DCloud_JSON's avatar
DCloud_JSON 已提交
21
async function realPreRegister(params = {}) {
22 23 24 25 26 27 28 29 30 31 32 33 34 35
  const {
    user
  } = params
  const userMatched = await findUser({
    userQuery: user,
    authorizedApp: this.getClientInfo().appId
  })
  if (userMatched.length > 0) {
    throw {
      errCode: ERROR.ACCOUNT_EXISTS
    }
  }
}

DCloud_JSON's avatar
DCloud_JSON 已提交
36
async function preRegister(params = {}) {
37 38 39 40 41 42 43 44 45 46 47
  try {
    await realPreRegister.call(this, params)
  } catch (error) {
    await this.middleware.uniIdLog({
      success: false,
      type: LOG_TYPE.REGISTER
    })
    throw error
  }
}

DCloud_JSON's avatar
DCloud_JSON 已提交
48
async function preRegisterWithPassword(params = {}) {
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  const {
    user,
    password
  } = params
  await preRegister.call(this, {
    user
  })
  const passwordUtils = new PasswordUtils({
    passwordSecret: this.config.passwordSecret
  })
  const {
    passwordHash,
    version
  } = passwordUtils.generatePasswordHash({
    password
  })
  const extraData = {
    password: passwordHash,
    password_secret_version: version
  }
  return {
    user,
    extraData
  }
}

DCloud_JSON's avatar
DCloud_JSON 已提交
75
async function thirdPartyRegister({
76 77 78 79 80 81 82 83
  user = {}
} = {}) {
  return {
    mobileConfirmed: !!(user.mobile && user.mobile_confirmed) || false,
    emailConfirmed: !!(user.email && user.email_confirmed) || false
  }
}

DCloud_JSON's avatar
DCloud_JSON 已提交
84
async function postRegister(params = {}) {
85 86 87 88 89 90 91 92 93 94 95 96 97 98
  const {
    user,
    extraData = {},
    isThirdParty = false,
    inviteCode
  } = params
  const {
    appId,
    appName,
    appVersion,
    appVersionCode,
    channel,
    scene,
    clientIP,
DCloud_JSON's avatar
DCloud_JSON 已提交
99 100
    osName,
    uniIdToken
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  } = this.getClientInfo()

  merge(user, extraData)

  const registerChannel = channel || scene
  user.register_env = {
    appid: appId || '',
    uni_platform: this.clientPlatform || '',
    os_name: osName || '',
    app_name: appName || '',
    app_version: appVersion || '',
    app_version_code: appVersionCode || '',
    channel: registerChannel ? registerChannel + '' : '', // channel可能为数字,统一存为字符串
    client_ip: clientIP || ''
  }

  user.register_date = Date.now()
  user.dcloud_appid = [appId]

  if (user.username) {
    user.username = user.username.toLowerCase()
  }
  if (user.email) {
    user.email = user.email.toLowerCase()
  }

  const {
    autoSetInviteCode, // 注册时自动设置邀请码
    forceInviteCode // 必须有邀请码才允许注册,注意此逻辑不可对admin生效
  } = this.config
  if (autoSetInviteCode) {
    user.my_invite_code = await getValidInviteCode()
  }

  const isAdmin = user.role && user.role.includes('admin')

  if (forceInviteCode && !isAdmin && !inviteCode) {
    throw {
      errCode: ERROR.INVALID_INVITE_CODE
    }
  }

  if (inviteCode) {
    const {
      inviterUid,
      inviteTime
    } = await generateInviteInfo({
      inviteCode
    })
    user.inviter_uid = inviterUid
    user.invite_time = inviteTime
  }

DCloud_JSON's avatar
DCloud_JSON 已提交
154 155 156 157 158 159
  if (uniIdToken) {
    try {
      await logout.call(this)
    } catch (error) {}
  }

160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
  const beforeRegister = this.hooks.beforeRegister
  let userRecord = user
  if (beforeRegister) {
    userRecord = await beforeRegister({
      userRecord,
      clientInfo: this.getClientInfo()
    })
  }

  const {
    id: uid
  } = await userCollection.add(userRecord)

  const {
    token,
    tokenExpired
  } = await this.uniIdCommon.createToken({
    uid
  })

  await this.middleware.uniIdLog({
    data: {
      user_id: uid
    },
    type: LOG_TYPE.REGISTER
  })

  return {
    errCode: 0,
    uid,
    newToken: {
      token,
      tokenExpired
    },
    ...(
      isThirdParty
        ? thirdPartyRegister({
          user: {
            ...userRecord,
            _id: uid
          }
        })
        : {}
    )
  }
}

module.exports = {
  preRegister,
  preRegisterWithPassword,
  postRegister
}