Last active
January 11, 2019 04:08
-
-
Save paulcsmith/5ca29744ab9ebfae88985394171ededf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SessionForm < LuckyRecord::VirtualForm | |
virtual email : String | |
virtual password : String | |
def submit | |
user = find_user | |
validate_required email, password | |
validate_user_and_password_match(user) | |
yield self, user | |
end | |
private def validate_user_and_password_match(user : User?) | |
# Usually ok to say what went wrong: https://github.com/luckyframework/lucky_cli/issues/192 | |
if user && !user.valid_password?(password.value.to_s) | |
password.add_error "is incorrect" | |
elsif user.nil? | |
email.add_error "was not found" | |
end | |
end | |
private def find_user | |
email.value.try do |value| | |
UserQuery.new.email(value).first? | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment