 
 _.extend Person, {
   login: (credentials, fn) ->
-    @first { email: /^#{credentials.email}$/i }, (error, person) ->
+    return fn ['Invalid email address'] unless validEmail credentials.email
+    @first { email: credentials.email.trim().toLowerCase() }, (error, person) ->
       return fn ['Unknown email'] unless person?
       return fn ['Invalid password'] unless person.passwordHash is md5 credentials.password
       person.token = Math.uuid()

    @save fn

  validate: ->
    ['Invalid email address'] unless /^[a-zA-Z0-9+._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test @email

  beforeSave: (fn) ->
    @email = @email?.trim()?.toLowerCase()

      #{if @password then 'password: ' + @password else 'and whatever password you already set'}

      You still need to complete your registration.
      Please sign in at: http://nodeknockout.com/login?email=#{escapeURL @email}&password=#{@password} to do so.


      Thanks!
