summaryrefslogtreecommitdiff
path: root/lib/lsg_web/controllers/untappd_controller.ex
blob: 1c3ceb154d4135af6188bb1fee13bc6457c4d7f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
defmodule LSGWeb.UntappdController do
  use LSGWeb, :controller

  def callback(conn, %{"code" => code}) do
    with \
         {:account, account_id} when is_binary(account_id) <- {:account, get_session(conn, :account)},
         {:account, account} when not is_nil(account) <- {:account, IRC.Account.get(account_id)},
         {:ok, auth_token} <- Untappd.auth_callback(code)
    do
      IRC.Account.put_meta(account, "untappd-token", auth_token)
      text(conn, "OK!")
    else
      {:account, _} -> text(conn, "Error: account not found")
      :error -> text(conn, "Error: untappd authentication failed")
    end
  end

end