Skip to content

Commit

Permalink
Part 4: Update create action
Browse files Browse the repository at this point in the history
  • Loading branch information
thebrianemory committed Jan 15, 2019
1 parent 0fa38b8 commit 2a442bd
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/catcasts_web/controllers/session_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule CatcastsWeb.SessionController do
use CatcastsWeb, :controller
plug(Ueberauth)

alias Catcasts.User
alias Catcasts.{Repo, User}

def create(%{assigns: %{ueberauth_auth: auth}} = conn, _params) do
user_params = %{
Expand All @@ -14,5 +14,28 @@ defmodule CatcastsWeb.SessionController do
}

changeset = User.changeset(%User{}, user_params)

case insert_or_update_user(changeset) do
{:ok, user} ->
conn
|> put_flash(:info, "Thank you for signing in!")
|> put_session(:user_id, user.id)
|> redirect(to: Routes.page_path(conn, :index))

{:error, _reason} ->
conn
|> put_flash(:error, "Error signing in")
|> redirect(to: Routes.page_path(conn, :index))
end
end

defp insert_or_update_user(changeset) do
case Repo.get_by(User, email: changeset.changes.email) do
nil ->
Repo.insert(changeset)

user ->
{:ok, user}
end
end
end

0 comments on commit 2a442bd

Please sign in to comment.