I'm trying to implement Guardian into my API and perform a login through it to get JWT back. The tutorial I'm looking at is here. The problem is implementing the login using a User Model similar to what they're using in the example. The Model code looks like:
defmodule PushflightServer.User do
use PushflightServer.Web, :model
use Ecto.Repo
import Ecto.Query
alias PushflightServer.Repo
schema "users" do
field :name, :string
field :email, :string
field :encrypted_password, :string
field :password, :string, virtual: true
field :verify_token, :string
field :verify_date, Ecto.DateTime
timestamps
end
def from_email(nil), do: { :error, :not_found }
def from_email(email) do
IO.write("Before email")
IO.inspect(email)
Repo.one(User, email: email)
end
If I call the from_email either from within Phoenix or straight in iex -S mix, I get the following error:
user = PushflightServer.User.from_email("[email protected]")
** (Protocol.UndefinedError) protocol Ecto.Queryable not implemented for User, the given module does not exist (ecto) lib/ecto/queryable.ex:33: Ecto.Queryable.Atom.to_query/1 (ecto) lib/ecto/repo/queryable.ex:90: Ecto.Repo.Queryable.execute/5 (ecto) lib/ecto/repo/queryable.ex:15: Ecto.Repo.Queryable.all/4 (ecto) lib/ecto/repo/queryable.ex:44: Ecto.Repo.Queryable.one/4
I must be missing something simple, but I haven't been able to find any documentation about why this is happening. The insert of the data using the Repo worked fine. Any ideas?
You need to fully namespace User
as PushflightServer.User
or you can use the shortcut __MODULE__
you should reference the modules with namespaces
def from_email(email) do
PushflightServer.one(PushflightServer.User, email: email)
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With