zanderxyz / veil

Simple passwordless authentication for your Phoenix apps
MIT License
166 stars 15 forks source link

Feature request - Authorised users #4

Closed kieranwild closed 6 years ago

kieranwild commented 6 years ago

It would be really helpful if you could document how to pull a list of Authorised Users from a .env file like {:system, "AUTH_USERS"} export AUTH_USERS= ("joe.blogs@example.com", "jane.doe@example.com") So only those users can login.

zanderxyz commented 6 years ago

Hi Kieran, sorry for the slow reply. This is a good idea, let me have a quick think and I’ll get back to you.

zanderxyz commented 6 years ago

I think the best way to handle this would be:

  1. Seed the database with your users in priv/repo/seeds.exs, and run mix run priv/repo/seeds.exs to insert them:
{:ok, _} = YourApp.Veil.create_user("joe.blogs@example.com")
{:ok, _} = YourApp.Veil.create_user("jane.doe@example.com")
  1. Update the create function in user_controller.ex so that only existing users can log in:
def create(conn, %{"user" => %{"email" => email}}) when not is_nil(email) do
    if user = Veil.get_user_by_email(email) do
      sign_and_email(conn, user)
    else
      render(conn, "new.html", changeset: User.changeset(%User{}))
    end
  end

Let me know what you think.