UserRegistrationHandler.coffee 952 B

123456789101112131415161718192021222324252627282930
  1. sanitize = require('validator').sanitize
  2. module.exports =
  3. validateEmail : (email) ->
  4. 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,}))$/
  5. return re.test(email)
  6. hasZeroLengths : (props) ->
  7. hasZeroLength = false
  8. props.forEach (prop) ->
  9. if prop.length == 0
  10. hasZeroLength = true
  11. return hasZeroLength
  12. validateRegisterRequest : (req, callback)->
  13. email = sanitize(req.body.email).xss().trim().toLowerCase()
  14. password = req.body.password
  15. username = email.match(/^[^@]*/)
  16. if username?
  17. first_name = username[0]
  18. else
  19. first_name = ""
  20. last_name = ""
  21. if @hasZeroLengths([password, email])
  22. callback('please fill in all the fields', null)
  23. else if !@validateEmail(email)
  24. callback('not valid email', null)
  25. else
  26. callback(null, {first_name:first_name, last_name:last_name, email:email, password:password})