Coherence — Devise for Phoenix

Rafał Mossakowski
3 min readAug 14, 2017

--

In this post I’ll try to briefly walk you through setting up user authentication system in Phoenix Framework using Coherence.

Before we start…

In this post I’m using Elixir v. 1.4.4 and Phoenix v. 1.3.0. If your versions differ and some commands return an error message, I strongly recommend you to check out official Coherence readme or look for tips & clues on StackOverflow.

You should also keep in mind that your project name may differ from the one I used in here and you should change your code accordingly.

Installing coherence

Let’s say you have generated your new, shiny phoenix project. You have run mix phx.new coherence_example, installed dependencies, created database and so on... Your project starts up successfully and shows you a nice welcome message typical for Phoenix framework. Now it’s time to get your hands dirty and add your User model.

Fire up your favourite editor and add coherence to dependencies section in your mix.exs file:

remember to run mix deps.get in order to fetch your new dependency.

Now it’s turn to install coherence. The easiest way to do this is to run mix coh.install --full-invitable command. This way coherence will take care of creating its configuration file, creating migrations, views, templates, email files etc.

Coherence did a great piece of work for us, but we still need to edit few files manually. Open up mix.exs and add coherence to list of extra applications:

Then, add few blocks of code to your router:

You may also want to add some user to your database seeds:

Now you should be able to run mix ecto.setup task, which will run your migrations and seed the database. If all those command success, you should be able to start your application with mix phx.server and - after navigating to localhost:4000/sessions/new- see the login form. For full list of available routes generated by coherence, run mix phx.routes from your project root directory.

Setting up emails

The last thing we need to do is adding email configuration, so we’ll be able to send invites to our new app (using invitable module). Open up config.exs and paste in your email_from_email, email_from_name and api_key:

Now, after navigating to /invitations/new path, and filling our the invitation form, your app will send an invitation email to an address given in a form. Keep in mind that this path is by default protected by coherence and will require you to sign in first.

What we’ve done so far

Thanks to the coherence and its generators we were able to generate our user model, scaffold basic views (like sign in, or registration forms) and create email templates, and it took only 5 minutes to do so… I highly recommend you to take a look at the code that was generated for us to take a deeper understanding of what is going on. If you already have some hands-on experience with Ruby on Rails framework, you will find a lot of similarities between coherence and devise gem. For those who want to learn about all features of the coherence package, I suggest going to its official readme, which can be found in here.

In my next blogpost I’ll try to walk you through adding and uploading user avatars using the arc package - stay tuned!

--

--