1
votes

I am trying to build a simple auth system in elixir/phoenix that can be used as a dependency in any phoenix application.

I have followed the steps taken in the programming-phoenix-1.4 book and set up a basic auth system easily enough.

In this book they talk about separating a users sensitive and non-sensitive information into different places in the application and database. All the non-sensitive info (username, name, etc) is put in the users table while the sensitive info (email, password, etc) is put in the credentials table.

Based on this, I wondered if it would be possible to separate out the auth logic into its own application.

This auth module will need to:

  1. create a credentials table in the database of the requiring application
  2. update said table based on info passed from the parent app
  3. contain an auth plug capable of updating the conn from the parent application (for example adding :current_user to the conn with the assign function)

Unfortunately I am falling at the first hurdle at the moment. I am not sure if it is possible to require a module that can create a database table.

I imagine that if step 1 is possible step 2 will be straightforward. Step 3 looks like it can be achieved with the Router.forward/4 function.

1
I am not sure what is that book mentioned, but I hardly understand the reasons or splitting user info into two separate tables. Also, how this module you propose would be better than battle-proved Guardian? - Aleksei Matiushkin
@AlekseiMatiushkin Thanks a lot for your response (and for all your other responses to elixir questions). I was mostly using this as a learning experience and wanted to know if this idea would be possible for me to build. I thought it would help create a simple signup/login/logout flow in any app I build following the lessons from programming phx 1.4. My goal wasn't to build something that I would use over something like Guardian in a "real" project. - RobStallion

1 Answers

1
votes

It's possible by giving the dependency the name of the repo module. An example of this is GuardianDB that creates a table to store valid tokens. The configuration for GuardianDB looks like this:

config :guardian, Guardian.DB,
  repo: MyApp.Repo,
  # default
  schema_name: "guardian_tokens",
  # default: 60 minutes
  sweep_interval: 60

you can use that library as a guide github

As for creating plugs, Guardian, which is an authentication library for Elixir does, that too. You can find the code here