dwyl/auth

Updated instructions needed for readme -- this app immediately crashes when I try to run it locally

tadasajon opened this issue ยท 10 comments

I cloned this project to my MacBook but couldn't get it running easily.

Screen Shot 2021-10-21 at 1 01 07 AM

These instructions fail because there is no assets/package.json file.

Ignoring assets, for the time being, even after setting an env based on your .env_sample file I can't start the app on localhost
Screen Shot 2021-10-21 at 1 06 37 AM
.

I think it is something to do with how the .env file is read. I assumed that I should source the env file -- i.e., make sure the variables were in the environment. But I think your code expects to actually read the variables from a file literally called ".env". So my approach of having ".env.prod" and ".env.dev", etc, doesn't work. priv/repo/seeds.exs seems to want to read a file called ".env". If that isn't the problem, then I've chased down the error I get when I start the app and try to visit localhost:4000 to a line in lib/auth/user_agent.ex -- screenshots attached

Screen Shot 2021-10-21 at 3 15 02 AM

Screen Shot 2021-10-21 at 3 15 59 AM

Hi @tadasajon thanks for opening this issue.

Concerning the assets:

Now that we have updated the project to use Phoenix 1.6 the npm install --prefix assets command might not be necessary as esbuild is used to create the assets instead of npm:

You can now build your js and css bundles without having node or npm on your system!

see the last section on https://www.phoenixframework.org/blog/phoenix-1.6-released

So we indeed need to update the Readme to reflect this change, ie mix deps.get instead of mix deps.get && npm install --prefix assets

Concerning the environment variables:

I assumed that I should source the env file -- i.e., make sure the variables were in the environment

You're right, I think we need to add a line in the environment variables section to describe how to do this. the command line source .env should work for linux and mac (forgot how to do this on Windows)

So my approach of having ".env.prod" and ".env.dev", etc, doesn't work

You're correct, the seed file expects to read the .env file, see:

auth/priv/repo/seeds.exs

Lines 83 to 99 in 3a9d687

def load_env() do
path = File.cwd!() <> "/.env"
IO.inspect(path, label: ".env file path")
{:ok, data} = File.read(path)
data
|> String.trim()
|> String.split("\n")
|> Enum.each(fn line ->
with line <- String.replace(line, ["export ", "'"], ""),
[key | rest] <- String.split(line, "="),
value <- Enum.join(rest, "=") do
System.put_env(key, value)
end
end)
end
end

This function is then call here:

auth/priv/repo/seeds.exs

Lines 15 to 18 in 3a9d687

def create_admin do
if is_nil(System.get_env("TRAVIS")) && is_nil(System.get_env("HEROKU")) do
load_env()
end

If the application isn't run on Travis (running tests) or Heroku (production application, we don't need .env as the environment variables are directly created in the Heroku application settings) then we read the .env file to create the admin user

I would suggest to create the .env file (and make sure all the variables are defined) on your application and run source .env before running mix phx.server.

Let us know if this help ๐Ÿ‘

Ok, I got to a 401 error -- Sorry, invalid AUTH_API_KEY. That is good progress.

ok, so I'm noticing that AUTH_API_KEY is not mentioned in your README. I think it would be helpful for me to have a slightly better understanding of what is going on -- perhaps there should be a section titled "a detailed look at how this app works and how it is initialized".

The README instructs me to run mix ecto.setup which causes the seeds.exs file to be run, and I think that is where AUTH_API_KEY should be initialized, but mix ecto.setup is also unfortunately crashing on me with:

** (Ecto.ConstraintError) constraint error when attempting to insert struct:

    * people_roles_person_id_role_id_app_id_index (unique_constraint)

Sheesh, this app just refuses to work for me. Now I can't get past lib/auth/person.ex: get_person_by_email/1 -- this function is invoked early on in priv/repo/seeds.ex but crashes with:

** (ArgumentError) errors were found at the given arguments:

  * 1st argument: not a bitstring

This typically happens when calling Kernel.bit_size/1 with an invalid argument or when performing binary construction or binary concatenation with <> and one of the arguments is not a binary
    :erlang.bit_size(nil)
    (fields 2.8.2) lib/helpers.ex:19: Fields.Helpers.get_salt/1
    (fields 2.8.2) lib/helpers.ex:15: Fields.Helpers.hash/2
    (fields 2.8.2) lib/hash.ex:22: Fields.Hash.dump/1
    (ecto 3.7.1) lib/ecto/type.ex:915: Ecto.Type.process_dumpers/3
    (elixir 1.13.1) lib/enum.ex:2396: Enum."-reduce/3-lists^foldl/2-0-"/3
    (elixir 1.13.1) lib/enum.ex:1715: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
    (elixir 1.13.1) lib/enum.ex:2396: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto 3.7.1) lib/ecto/repo/queryable.ex:203: Ecto.Repo.Queryable.execute/4

The offending line appears to be:

Repo.get_by(email_hash: email)

So I have not yet been able to get past mix ecto.setup, lol

Can confirm this isn't working on a fresh git clone for me either

I've just try the running the application again from a fresh git clone and it sees to be working fine for me.
Maybe the error is due to the environment variables not loaded/defined.
@tadasajon Have you try running source .env to make sure the ADMIN_EMAIL value is created, see https://github.com/dwyl/auth/blob/main/.env_sample ?

@SimonLab Yes, I have sourced the .env file and I have set ADMIN_EMAIL.

I am not sure what I should set in the .env file for AUTH_API_KEY, however.

I cannot get past the setup step in which the priv/repo/seeds.exs file is executed.

I just tried running mix ecto.reset again and it is crashing in create_apikey_for_admin(person).

These lines are throwing an error:

# set the api key to AUTH_API_KEY in env:
update_attrs = %{
  "client_id" => AuthPlug.Token.client_id(),
  "client_secret" => AuthPlug.Token.client_secret()
}

The error is: (MatchError) no match of right hand side value: nil.

Can you tell me what I should put in my .env file for the AUTH_API_KEY?

By the way, I do include the AUTH_API_KEY in my .env file as follows:

export AUTH_API_KEY='not set yet!'

So it should at least exist.

Also, it seems circular for this app to require me to provide it with an AUTH_API_KEY -- isn't it supposed to be the other way around? Should I be requiring this app to provide me with an AUTH_API_KEY?