# Elixir / Phoenix login controller, allowing multiple attempts

I have a login method in my controller. I would like to get rid of the imperative "thinking" and write something more functional.

  def login(conn, %{"login" => %{"password" => password}}) do

conn = conn |> fetch_session

conn
else
conn
|> redirect(to: "/")
end

end

end

conn
|> redirect(to: "/dashboard")
end

conn
|> put_flash(:error, "Impossible to connect with this password")
|> redirect(to: "/dashboard/auth")
end


Personally I would do the inc and count of the attempts in one place, You can still split it, but you actually need this every time an attempt is going to happen.

You could also replace the if with a case. You don't have to, but I think it looks nicer :)

The next thing is, remove the when password == @password_login and just use @password_login in the function definition

  def login(conn, %{"login" => %{"password" => password}}) do
_ ->     conn |> redirect(to: "/")
end
end

attempts = (conn |> get_session(:count_attempt_login)) || 0
attempts + 1
end