4

我正在尝试为身份验证登录创建一个 mixins,因此它可以应用于我应该能够登录的模型。很像 Ruby 中的 has_secure_password。

Afaik 这是使用use必需模块的语句完成的,并调用__using__宏。所以我像这样实现了我的mixin。

defmodule MyApp.SecurePassword do
  defmacro __using__(_options) do
    quote do
      import MyApp.SecurePassword
    end
  end

  defmacro authenticate(password) do
    # Lets return true, for testing purposes.
    true
  end
end

然后我在我的“用户”模型中调用使用。

defmodule MyApp.Farm do
  use MyApp.Web, :model
  use MyApp.SecurePassword

  schema "farms" do
    field :name, :string
    field :email, :string
  #.....

在我的控制器中,我正在尝试使用该方法。

 def create(conn, %{"session" => session_params}) do
    user = Repo.get_by(Farm, email: session_params["email"])

    if user && user.authenticate(session_params["password"]) do
      conn = put_flash(conn, :success, "You were successfully logged in")
    else
      conn = put_flash(conn, :error, "Credentials didn't match")
    end

    redirect(conn, to: session_path(conn, :new))
 end

但是当我点击代码时,我只是在调用验证函数的那一行得到一个参数错误。

我的宏观技能比较弱,我做错了什么?:)

4

1 回答 1

6

我想你想要的是调用authenticate传入用户和密码的函数:

def authenticate(user, password) do
  # auth logic
end

接着:

import MyApp.SecurePassword
# ...
if user && authenticate(user, session_params["password"]) do
# ...

现在似乎没有任何理由使用宏或 a use,一个简单的import就可以了 - 你只需要那些在生成一些代码编译时,在这种情况下,你想要的一切似乎都将在运行时发生。

于 2015-05-16T09:16:50.563 回答