Hi all,
I’m a newbie with Elixir (Phoenix) so I’m having some trouble. Basically, I’m trying to create new user through API and get the token from Guardian package. I have setup mongo_db
driver which should to my knowledge provide most of the functionality the normal Repo
should. However, I’m unable to insert documents using the code below:
def create(conn, %{ "user" => user_params }) do
changeset = User.changeset(%User{}, user_params)
case Repo.insert(changeset) do
{:ok, user} ->
new_conn = Guardian.Plug.api_sign_in(conn, user, :access)
jwt = Guardian.Plug.current_token(new_conn)
new_conn
|> put_status(:created)
|> render(TrackerApiWeb.SessionView, "show.json", user: user, jwt: jwt)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(TrackerApiWeb.ChangesetView, "error.json", changeset: changeset)
end
end
Meanwhile, if I use the following I’m able to insert document but with some missing validation rules and again no token generated:
case Mongo.insert_one(:mongo, "users", changeset_new_user.changes, [return_document: :after, upsert: :true]) do
{:ok, user} ->
new_conn = Guardian.Plug.api_sign_in(conn, user, :access)
jwt = Guardian.Plug.current_token(new_conn)
new_conn
|> put_status(:created)
|> render(TrackerApiWeb.SessionView, "show.json", user: user, jwt: jwt)
{:error, changeset} ->
conn
|> put_status(:unprocessable_entity)
|> render(TrackerApiWeb.ChangesetView, "error.json", changeset: changeset)
end
I have been trying to debug the issue, google it but with no results. I do like Phoenix but compatibility and missing documentation especially on how to work with MongoDB it is making it harder to progress.
Here is also my mix.exs
file:
def application do
[
mod: {TrackerApi.Application, []},
applications: [
:phoenix, :phoenix_pubsub, :gettext,
:phoenix_ecto, :mongodb, :mongodb_ecto, :ecto,
:cowboy, :logger,
:poolboy, :uuid,
:comeonin, :scrivener_ecto, :corsica
],
extra_applications: [:runtime_tools]
]
end
# Specifies which paths to compile per environment.
defp elixirc_paths(:test), do: ["lib", "test/support"]
defp elixirc_paths(_), do: ["lib"]
# Specifies your project dependencies.
#
# Type `mix help deps` for examples and options.
defp deps do
[
{:phoenix, "~> 1.2.1"},
{:plug, "~>1.3.5", override: true},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, github: "phoenixframework/phoenix_ecto", ref: "v3.0.1"},
{:mongodb, ">= 0.0.0"},
{:mongodb_ecto, github: "zeroasterisk/mongodb_ecto", branch: "elixir-ecto-v2.0.1"},
{:ecto, "~> 2.0"},
{:gettext, "~> 0.11"},
{:uuid, "~> 1.1" },
{:timex, "~> 3.0"},
{:cowboy, "~> 1.0"},
{:comeonin, "~> 2.5"},
{:guardian, "~> 0.13.0"},
{:corsica, "~> 0.5.0"},
{:scrivener_ecto, "~> 1.0"}
]
end
Any help appreciated