ueberauth / guardian

Elixir Authentication
MIT License
3.44k stars 381 forks source link

Guardian.Plug.sign_in -> "argument error" #489

Closed flomaster9 closed 6 years ago

flomaster9 commented 6 years ago

When i try to use Guardian.Plug.sign_in while testing, terminal tell me this

** (UndefinedFunctionError) function Guardian.Plug.sign_in/2 is undefined or private. Did you mean one of:

       * sign_in/3
       * sign_in/4
       * sign_in/5

but when i write before, in the top of file

use Guardian.Plug

The sign_in/2 is available, and i see this

 ** (ArgumentError) argument error

Here is my Api.Auth.login

def login(conn, user) do conn |> sign_in(user) |> assign(:current_user, user) end

Here is config

config :api, Api.Auth.Guardian, issuer: "api", secret_key: "HNinpKh9Ne3tr8BpjCpAEh0xzCqTIG3PWsfkR2AtzvUaRIpbs6oIQ9RcmjmGPekJ"

And guardian callbacks is:

defmodule Rumbl.Auth.Guardian do

     use Guardian, otp_app: :api

     def subject_for_token(user, _claims) do
          sub = to_string(user.id)
          {:ok, sub}
      end

      def resource_from_claims(claims) do
          id = claims["sub"]
          user = Api.Repo.get(Api.User, id)
          {:ok, user}
       end

end

I read guide step by step, but something went wrong...

ghost commented 6 years ago

Maybe I can help you out (I'm not sure if this is 100% correct). You don't have to put use Guardian.Plug on top if the file. Replace that line with alias Rumble.Auth.Guardian and call sign_in with Guardian.Plug.sign_in(user). That worked in my case: https://github.com/hlhr/phoenix_authentication/blob/master/lib/phoenix_authentication_web/controllers/auth_controller.ex .

flomaster9 commented 6 years ago

This work !! thanks ) I will close issue