| 223 | }, |
| 224 | |
| 225 | signup(appId, document, accessList, isMasterKey, encryption_key) { |
| 226 | const deferred = q.defer(); |
| 227 | try { |
| 228 | customService.findOne(appId, Collections.User, { |
| 229 | username: document.username, |
| 230 | }, null, null, null, accessList, isMasterKey).then((user) => { |
| 231 | if (user) { |
| 232 | deferred.reject('Username already exists'); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | customService.save(appId, Collections.User, document, accessList, isMasterKey, null, encryption_key).then((user) => { |
| 237 | // Send an email to activate account. |
| 238 | const cipher = crypto.createCipher('aes192', config.secureKey); |
| 239 | let activateKey = cipher.update(user._id, 'utf8', 'hex'); |
| 240 | activateKey += cipher.final('hex'); |
| 241 | |
| 242 | const promises = []; |
| 243 | promises.push(appService.getAllSettings(appId)); |
| 244 | promises.push(mailService.sendSignupMail(appId, user, activateKey)); |
| 245 | |
| 246 | q.all(promises).then((list) => { |
| 247 | const auth = _.first(_.where(list[0], { |
| 248 | category: 'auth', |
| 249 | })); |
| 250 | let signupEmailSettingsFound = false; |
| 251 | let allowOnlyVerifiedLogins = false; |
| 252 | |
| 253 | if (auth && auth.settings && auth.settings.signupEmail) { |
| 254 | signupEmailSettingsFound = true; |
| 255 | if (auth.settings.signupEmail.allowOnlyVerifiedLogins) { |
| 256 | allowOnlyVerifiedLogins = true; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | if (signupEmailSettingsFound && allowOnlyVerifiedLogins) { |
| 261 | if (user.verified) { |
| 262 | deferred.resolve(user); |
| 263 | } else { |
| 264 | deferred.resolve(null); |
| 265 | } |
| 266 | } else { |
| 267 | deferred.resolve(user); |
| 268 | } |
| 269 | }, (error) => { |
| 270 | deferred.reject(error); |
| 271 | }); |
| 272 | }, (error) => { |
| 273 | deferred.reject(error); |
| 274 | }); |
| 275 | }, (error) => { |
| 276 | deferred.reject(error); |
| 277 | }); |
| 278 | } catch (err) { |
| 279 | winston.log('error', { |
| 280 | error: String(err), |
| 281 | stack: new Error().stack, |
| 282 | }); |