defmodule RemoteRetro.AuthController do
  use RemoteRetro.Web, :controller
  alias RemoteRetro.OAuth.Google
  alias RemoteRetro.User

  def index(conn, _params) do
    redirect conn, external: authorize_url!()
  end

  def callback(conn, %{"code" => code}) do
    user_info = Google.get_user_info!(code)
    user = Repo.get_by(User, email: user_info["email"])

    user_params = User.build_user_from_oauth(user_info)

    user = if !user do
      changeset = User.changeset(%User{}, user_params)
      Repo.insert!(changeset)
    else
      changeset = User.changeset(user, user_params)
      Repo.update!(changeset)
    end

    user = Map.delete(user, :__meta__)
    user = Map.delete(user, :__struct__)

    conn = put_session(conn, :current_user, user)

    redirect conn, to: get_session(conn, "requested_endpoint") || "/"
  end

  defp authorize_url! do
    Google.authorize_url!(scope: "email profile")
  end
end

defmodule RemoteRetro.AuthController do
  use RemoteRetro.Web, :controller
  alias RemoteRetro.OAuth.Google
  alias RemoteRetro.User

  def index(conn, _params) do
    redirect conn, external: authorize_url!()
  end

  def callback(conn, %{"code" => code}) do
    user_info = Google.get_user_info!(code)
    user_params = User.build_user_from_oauth(user_info)

    {:ok, user} =
      case Repo.get_by(User, email: user_info["email"]) do
        nil -> %User{}
        user_from_db -> user_from_db
      end
      |> User.changeset(user_params)
      |> Repo.insert_or_update

    user =
      user
      |> Map.delete(:__meta__)
      |> Map.delete(:__struct__)

    conn = put_session(conn, :current_user, user)

    redirect conn, to: get_session(conn, "requested_endpoint") || "/"
  end

  defp authorize_url! do
    Google.authorize_url!(scope: "email profile")
  end
end

Modify AuthController to use insert_or_update
Modify AuthController to use insert_or_update

Work on #136

stride-nyc/remote_retro,samdec11/remote_retro,stride-nyc/remote_retro,tnewell5/remote_retro,stride-nyc/remote_retro,tnewell5/remote_retro