summaryrefslogtreecommitdiff
path: root/lib/web/controllers/untappd_controller.ex
blob: e2e1596efc3ea310d4a8cb67bec45a5447d3716c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
defmodule NolaWeb.UntappdController do
  use NolaWeb, :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, Nola.Account.get(account_id)},
         {:ok, auth_token} <- Untappd.auth_callback(code)
    do
      Nola.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