concept Auction in category elixir

appears as: Auction, Auction, n Auction
Phoenix in Action

This is an excerpt from Manning's book Phoenix in Action.

In the Auction module, add the following function definition.

Listing 4.5. Defining Auction.FakeRepo.get!/2
def get!(Item, id) do
  Enum.find(@items, fn(item) -> item.id === id end)
end

The first step you need to take in this chapter is to define what a user is. Will they have usernames or just an email address? What information will you collect about them? These are decisions that you need to make before you allow user registration. And for the most part, these choices will be implemented on the Auction side of your umbrella application (as opposed to the Phoenix-focused AuctionWeb side).

There are three things you need to do in Auction before you move back over to Phoenix:

To look up a user based on the submitted username and password, you also need to implement a new function in the Auction API module. This function should return the Auction.User if one is found, or return false (or, really, anything else) if one isn’t found.

In keeping with the spirit of having your modules worry about as few things as possible, you’ll offload the password-hashing activities to the Auction.Password module. You’ll implement those functions momentarily. For now, let’s write the other functionality in the Auction.get_user_by_username_and_password/2 function.

Listing 10.19. Implementing Auction.get_user_by_username_and_password/2
defmodule Auction do
  alias Auction.{Repo, Item, User, Password}               #1
  @repo Repo
  # ...

  def get_user_by_username_and_password(username, password) do
    with user when not is_nil(user) <- @repo.get_by(User, %{username:
     username}),
         true <- Password.verify_with_hash(password, user.hashed_password) do
      user
    else
      _ -> Password.dummy_verify
    end
  end
end

You now know enough about how your functions will work that you can implement Auction.Password.verify_with_hash/2 and Auction.Password.dummy_verify/0. Just like Auction.Password.hash/1, your functions will be simple wrappers around functions provided by your hashing package of choice, pbkdf2_elixir. The following listing shows the full module.

Listing 10.20. Implementing more of Auction.Password
defmodule Auction.Password do
  import Pbkdf2                                  #1

  def hash(password), do:
    hash_pwd_salt(password)                      #2

  def verify_with_hash(password, hash), do:
    verify_pass(password, hash)                  #2

  def dummy_verify, do: no_user_verify()         #2
end