Skip to content

Instantly share code, notes, and snippets.

@paulcsmith
Last active Jan 11, 2019
Embed
What would you like to do?
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