I’ve been working through the Phx 1.3 docs and have been upgrading an that uses Ueberauth OAuth strategies + Guardian for authentication and tokens, but I’ve been running into some errors with callback/changesets after shifting over to the contexts paradigm
Can anyone point to a good implementation/example of Ueberauth OAuth or Identity in Phoenix 1.3?
As for my error, if any of ya’ll are interested…
I’ve created my schema and account logic using mix phx.gen.context Accounts Users etc etc, and I’m not quite sure of how to alias my Accounts Users schema and logic and how to operate on it in my controller, or if I shouldn’t implement it on my controller anymore…
I currently alias it in my controller as:
alias Auth.Accounts.User
in my AuthController, but on mix ecto.create I get the following error:
== Compilation error in file lib/auth_web/controllers/auth_controller.ex ==
** (CompileError) lib/auth_web/controllers/auth_controller.ex:13: Auth.Accounts.User.__struct__/1 is undefined, cannot expand struct Auth.Accounts.User
(stdlib) lists.erl:1354: :lists.mapfoldl/3
lib/auth_web/controllers/auth_controller.ex:7: (module)
(stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
Full controller code:
defmodule Auth.AuthController do
use AuthWeb, :controller
plug Ueberauth
alias Auth.Accounts.User
def callback(%{assigns: %{ueberauth_auth: auth}} = conn, params) do
IO.inspect(conn)
user_params = %{
token: auth.credentials.token,
email: auth.info.email,
provider: auth.provider}
changeset = User.changeset(%User{}, user_params)
signin(conn, changeset)
end
def signout(conn, _params) do
conn
|> configure_session(drop: true)
|> redirect(to: page_path(conn, :index))
end
defp signin(conn, changeset) do
case insert_or_update_user(changeset) do
{:ok, user} ->
conn
|> put_flash{:info, "Welcome!"}
|> put_session(:user_id, user.id)
|> redirect(to: page_path(conn, :index))
{:error, _reason} ->
conn
|> put_flash(:error, "A Sign-in Error Occurred")
|> redirect(to: 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