mirror of
https://github.com/overleaf/overleaf.git
synced 2024-10-31 21:21:03 -04:00
64 lines
2 KiB
CoffeeScript
64 lines
2 KiB
CoffeeScript
sanitize = require('sanitizer')
|
|
User = require("../../models/User").User
|
|
UserCreator = require("./UserCreator")
|
|
AuthenticationManager = require("../Authentication/AuthenticationManager")
|
|
NewsLetterManager = require("../Newsletter/NewsletterManager")
|
|
async = require("async")
|
|
logger = require("logger-sharelatex")
|
|
|
|
module.exports =
|
|
validateEmail : (email) ->
|
|
re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\ ".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA -Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
|
return re.test(email)
|
|
|
|
hasZeroLengths : (props) ->
|
|
hasZeroLength = false
|
|
props.forEach (prop) ->
|
|
if prop.length == 0
|
|
hasZeroLength = true
|
|
return hasZeroLength
|
|
|
|
_registrationRequestIsValid : (body, callback)->
|
|
email = sanitize.escape(body.email).trim().toLowerCase()
|
|
password = body.password
|
|
username = email.match(/^[^@]*/)
|
|
if @hasZeroLengths([password, email])
|
|
return false
|
|
else if !@validateEmail(email)
|
|
return false
|
|
else
|
|
return true
|
|
|
|
_createNewUserIfRequired: (user, userDetails, callback)->
|
|
if !user?
|
|
UserCreator.createNewUser {holdingAccount:false, email:userDetails.email}, callback
|
|
else
|
|
callback null, user
|
|
|
|
registerNewUser: (userDetails, callback)->
|
|
self = @
|
|
requestIsValid = @_registrationRequestIsValid userDetails
|
|
if !requestIsValid
|
|
return callback(new Error("request is not valid"))
|
|
userDetails.email = userDetails.email?.trim()?.toLowerCase()
|
|
User.findOne email:userDetails.email, (err, user)->
|
|
if err?
|
|
return callback err
|
|
if user?.holdingAccount == false
|
|
return callback(new Error("EmailAlreadyRegistered"))
|
|
self._createNewUserIfRequired user, userDetails, (err, user)->
|
|
if err?
|
|
return callback(err)
|
|
async.series [
|
|
(cb)-> User.update {_id: user._id}, {"$set":{holdingAccount:false}}, cb
|
|
(cb)-> AuthenticationManager.setUserPassword user._id, userDetails.password, cb
|
|
(cb)->
|
|
NewsLetterManager.subscribe user, ->
|
|
cb() #this can be slow, just fire it off
|
|
], (err)->
|
|
logger.log user: user, "registered"
|
|
callback(err, user)
|
|
|
|
|
|
|
|
|