Hello everyone!
I am currently on Phoenix 1.4.4
trying to use Joken 1.5
in Phoenix 1.4.4
but using Jason 1.1
instead of Poison
.
{:jason, "~> 1.1"},
{:ja_serializer, "~> 0.14.1"},
{:bcrypt_elixir, "~> 2.0"},
{:joken, "~> 1.5.0"}
Got the following code:
views/session_view.ex
defmodule LibraryApiWeb.SessionView do
use LibraryApiWeb, :view
def render("token.json", user) do
data = %{id: user.id, email: user.email, username: user.username}
jwt = %{data: data, sub: user.id}
|> Joken.token
|> Joken.with_signer(Joken.hs512("SOMESECRETVALUE"))
|> Joken.sign
IO.inspect jwt
%{token: jwt.token}
end
end
controllers/session_controller.ex
defmodule LibraryApiWeb.SessionController do
use LibraryApiWeb, :controller
alias LibraryApi.Library
alias LibraryApi.Library.User
def create(conn, %{"email" => email, "password" => password}) do
try do
user = Library.get_user_by_email!(email)
conn
|> render("token.json", user)
rescue
_e ->
conn
|> put_status(:unauthorized)
|> render(LibraryApiWeb.ErrorView, "401.json-api", %{detail: "Error logging in a user with that email and password"})
end
end
end
views/error_view.ex
defmodule LibraryApiWeb.ErrorView do
use LibraryApiWeb, :view
def render("400.json-api", %Ecto.Changeset{} = changeset) do
JaSerializer.EctoErrorSerializer.format(changeset)
end
def render("401.json-api", %{detail: detail}) do
%{status: 401, title: "Unauthorized", detail: detail}
|> JaSerializer.ErrorSerializer.format()
end
def render("404.json-api", _assigns) do
%{title: "Page not found", status: 404}
|> JaSerializer.ErrorSerializer.format()
end
def render("500.json-api", _assigns) do
%{title: "Internal server error", status: 500}
|> JaSerializer.ErrorSerializer.format()
end
def template_not_found(_template, assigns) do
render "500.json-api", assigns
end
end
I am trying to do a POST
in Postman with the following which is existing in the database:
{
"email": "jack@hello.com",
"password": "asdf"
}
And I am getting the following error in Postman:
{
"errors": [
{
"status": 500,
"title": "Internal server error"
}
],
"jsonapi": {
"version": "1.0"
}
}
And the following in my iex console:
iex(1)> [error] GenServer :jose_server terminating
** (UndefinedFunctionError) function Poison.encode!/1 is undefined (module Poison is not available)
Was expecting a token…
Does this mean that Joken
is dependent on Poison
and I can’t use Jason
?
Would love to hear anybody’s advice on how to move forward with this coz this has been a blocker for me for more than a day already