defmodule RecycledCloud.Captcha do # We can't use a graphical-only captcha library such as # https://hex.pm/packages/captcha since it would break accessibility for # blind people. Let's make it simple maths instead! def validate(changeset, expected_result) do captcha_result = Ecto.Changeset.get_field(changeset, :captcha) if captcha_result == expected_result do changeset else Ecto.Changeset.add_error(changeset, :captcha, "does not match") end end def generate do terms = 1..6 operands = ["+", "-", "*"] left = terms |> Enum.random right = terms |> Enum.random operation = operands |> Enum.random text = "#{left} #{operation} #{right}" value = case operation do "+" -> left + right "-" -> left - right "*" -> left * right end {text, value} end end