ueberauth/guardian

UserManager.change_user() ?

7stud opened this issue · 3 comments

7stud commented

(1)
The only function that the tutorial puts in lib/auth_me/user_manager.ex is:

  def authenticate_user(username, plain_text_password) do
    query = from u in User, where: u.username == ^username
    case @repo.one(query) do
      nil ->
        Argon2.no_user_verify()
        {:error, :invalid_credentials}
      user ->
        if Argon2.verify_pass(plain_text_password, user.password) do
          {:ok, user}
        else
          {:error, :invalid_credentials}
        end
    end
  end

Yet, in lib/auth_me_web/controllers/session_controller.ex the tutorial calls the function:

    UserManager.change_user(%User{})

here:

defmodule AuthMeWeb.SessionController do
  use AuthMeWeb, :controller

  alias AuthMe.{UserManager, UserManager.User, UserManager.Guardian}

  def new(conn, _) do
    changeset = UserManager.change_user(%User{})  ### <===== HERE 

Is AuthMe.UserManager.change_user() simply a wrapper around AuthMe.UserManager.User.changeset(), for instance:

def change_user(user, attrs \\ %{}), do: AuthMe.UserManager.User.changeset(user, attrs)

(2)
In lib/auth_me/user_manager.ex:

  def authenticate_user(username, plain_text_password) do
    query = from u in User, where: u.username == ^username

the query line will throw an error without:

 import Ecto.Query, only: [from: 2]

(3)
The tutorial calls another undefined function in the Try it Out section:

Create the user:
AuthMe.UserManager.create_user(%{username: "me", password: "secret"})

Hi @7stud. Thank you for question.
I can see it can be a bit confusing, but the missing functions are actually generated by Phoenix when running mix phx.gen.context UserManager User users username:string password:string, what is command does; is create a new User in the UserManager context, and it is in this context the functions are auto generated. I hope this makes sense

7stud commented

Okay, thanks for the tips. I added the two required functions to my UserManager context. However, there are a few other errors in the tutorial that will keep any beginner from ever getting the Guardian example to work. I had to dig through the source code and follow a trail through two macros to figure out one error in the tutorial. I am going to submit a pull request to fix the tutorial.

I am sorry to hear that there are other problems with the tutorial, as stated above those two functions should be auto generated, when the tutorial ask you to run mix phx.gen.context UserManager User users username:string password:string. If there are any other problems just let us know and we will have a look at them