Elixir / Phoenix New Project Comeonin.Bcrypt不可用

时间:2017-05-21 17:05:49

标签: elixir phoenix-framework bcrypt

首先,我在Windows 10中进行开发。我在每次编译之前运行vcvarsall.bat amd64。我正在使用:

  • Elixir 1.4.2

  • Phoenix v1.2.1

我开始了一个全新的项目,制作了一个用户表,一切都运行良好。我添加了comeonin哈希pw,我不能再创建用户。我得到一个错误页面说:
 function Comeonin.Bcrypt.hashpwsalt/1 is undefined (module Comeonin.Bcrypt is not available)

以下是相关文件的代码:

Mix.exs

# mix.exs

 ...

  def application do
    [mod: {PollarAppV2, []},
     applications: [:phoenix, :phoenix_pubsub, :phoenix_html, :cowboy, :logger, :gettext,
     :phoenix_ecto, :postgrex, :comeonin, :timex]]
  end

 ...

  defp deps do
    [{:phoenix, "~> 1.2.1"},
     {:phoenix_pubsub, "~> 1.0"},
     {:phoenix_ecto, "~> 3.0"},
     {:postgrex, ">= 0.0.0"},
     {:phoenix_html, "~> 2.6"},
     {:phoenix_live_reload, "~> 1.0", only: :dev},
     {:gettext, "~> 0.11"},
     {:cowboy, "~> 1.0"},
     {:comeonin, "~> 3.0"},
     {:timex, "~> 3.0"}]
  end

 ...

User.ex

# user.ex

...

  defp generate_password_hash(changeset) do
    case changeset do
      %Ecto.Changeset{valid?: true, changes: %{password: password}} ->
        put_change(changeset, :encrypted_password, Comeonin.Bcrypt.hashpwsalt(password))
      _ ->
        changeset
    end
  end

当我尝试保存调用generate_password_hash的用户时,我收到此错误:

控制台

[warn] The on_load function for module Elixir.Comeonin.Bcrypt returned { :error,
 {:load_failed,
  'Failed to load NIF library c:/code/phoenix/pollar_app_v2/_build/dev/lib/comeonin/priv/bcrypt_nif: \'Unspecified error\''}}

[info] Sent 500 in 16ms
[error] #PID<0.430.0> running PollarAppV2.Endpoint terminated
Server: localhost:4000 (http)
Request: POST /users
** (exit) an exception was raised:
    ** (UndefinedFunctionError) function Comeonin.Bcrypt.hashpwsalt/1 is 
undefined (module Comeonin.Bcrypt is not available)
        (comeonin) Comeonin.Bcrypt.hashpwsalt("asdfasdf")
        (pollar_app_v2) web/models/user.ex:40: PollarAppV2.User.generate_password_hash/1
        (pollar_app_v2) web/controllers/user_controller.ex:17: PollarAppV2.UserController.create/2
        (pollar_app_v2) web/controllers/user_controller.ex:1: PollarAppV2.UserController.action/2
        (pollar_app_v2) web/controllers/user_controller.ex:1: PollarAppV2.UserController.phoenix_controller_pipeline/2
        (pollar_app_v2) lib/pollar_app_v2/endpoint.ex:1: PollarAppV2.Endpoint.instrument/4
        (pollar_app_v2) lib/phoenix/router.ex:261: PollarAppV2.Router.dispatch/2
        (pollar_app_v2) web/router.ex:1: PollarAppV2.Router.do_call/2
        (pollar_app_v2) lib/pollar_app_v2/endpoint.ex:1: PollarAppV2.Endpoint.phoenix_pipeline/1
        (pollar_app_v2) lib/plug/debugger.ex:123: PollarAppV2.Endpoint."call 
(overridable 3)"/2  
        (pollar_app_v2) lib/pollar_app_v2/endpoint.ex:1:  PollarAppV2.Endpoint.call/2
        (plug) lib/plug/adapters/cowboy/handler.ex:15: Plug.Adapters.Cowboy.Handler.upgrade/4
        (cowboy) 
        c:/code/phoenix/pollar_app_v2/deps/cowboy/src/cowboy_protocol.erl:442: :cowboy_protocol.execute/4  

我已经多次运行mix deps.clean --allmix deps.update --allmix deps.compilemix compile等。它永远不会指示nif文件没有编译,我可以在文件结构中看到正确位置的文件,但我无法访问应用程序中的Bcrypt。关于如何解决这个问题的任何想法?

2 个答案:

答案 0 :(得分:6)

我遇到了同样的问题。从the repo's readme开始,据说您需要为选择的算法添加正确的库。在你的情况下:

<div> <strong>Entries:</strong> <div id="entries" style="border: thin solid"> <%= f.fields_for :entries do |oi| %> <%= render "entry_fields", f: oi %> <% end %> <div class="links"> <%= link_to_add_association 'Add Entry', f, :entries, {id: 'cocoon-add-entry'} %> </div> </div> </div>

它为我解决了这个问题。

答案 1 :(得分:0)

这是因为来自Bcrypt的跨平台问题,必须从C编译。基本上,64位Erlang不能使用32位dll。

解决它的第一步是安装Visual Studio Community Edition 2015或更高版本。然后为它安装C ++构建工具。

接下来,打开VS2015&#34;的开发人员命令提示符。它将在当前目录中打开提示(例如C:\Program Files (x86)\Microsoft Visual Studio 14.0\)。

VC子目录中,应该有一个名为vcvarsall.bat的文件。 cd进入该目录并传递vcvarsall.bat命令参数,将编译目标设置为64位:

cd vc
vcvarsall.bat amd64

接下来,回到凤凰城的目录,清理你为comeonin和重新编译而构建的任何内容。

cd c:\path to\my phoenix project
mix deps.clean comeonin
mix deps.update comeonin
mix deps.compile comeonin

然后你应该为你的平台提供一个有效的Bcrypt NIF。