Rewriting elixirstream.com from Rails to Phoenix
By: Janis Miezitis
Tags: Elixir, Web Development

The title seems a little bit strange at first but just hang on here for a second, I will explain it bit by bit. We launched http://elixirstream.com with existing code base from http://emberflare.com to have a blog post and article sharing platform for fellow Alchemists. The code base itself was written in Rails and we wanted to launch it as quickly as possible bearing in mind that we will rewrite it to Phoenix and Elixir as soon as we can. And what you know, it happened! The purpose of this blog post is to share my findings in regards to Phoenix as a framework from a technical standpoint and give a brief overview of user authentication process, which covers most of the necessary parts of the web development and will allow you to start building “real world” apps. I want to warn you that is is my first Phoenix app so some of my understanding about the conventions might be wrong. Anyways, the critique is always welcome. :)

Registration process

The first bit of the application I wanted to tackle was the registration and logging in of the user.In the previous Rails app we used has_secure_password that is built in Rails to hash our passwords. Under the good it uses the Bcrypt encryption algorithm that generates different hashes each time you do an encryption, yet allowing to compare them for equality. For Elixir side of things, I found an amazing library to do password encryption and compare the encrypted password to the unencrypted counterpart: http://hexdocs.pm/comeonin/. I checked it against my own encrypted password and it worked like a charm. Let’s take a look at the users model:

The purpose of the ecto model is to define validations and to transport the data. I really enjoy the concept of changesets. Once you call the following code:

The changeset is being created with the name of our model as a first argument and the parameters for a given model as a second argument. Both required and optional parameters are being cast to corresponding model columns, or more precisely, values on our %User{} struct. After that is done, we simply pipe the changeset to a chain of validation functions. One thing to note here, that Ecto can also be validated the data against our repository (database) like so:

After the data has gone trough the validations, the changeset is being returned to us. It is either valid or invalid. The thing to note here is that User does not store the actual password in the database, that’s why we had to use a virtual attribute on the model to achieve that. The line field :password, :string, virtual: true allows us to set this attribute, yet not store it in the database on the insert or update. The line use Ecto.Model.Callbacks allows us to define callback functionality to our models. We set the password digest at before_insert :set_password_digest. This made me cringe at first, and if you check commit history, you can see that my attempt was to avoid callbacks at any cost. The reason behind my cringing is simple — I have been bitten by ActiveRecord callbacks multiple times, but this is different since this is the data that has to be there at any insert and after a brief discussion with my colleague, we agreed to use callbacks password hashing. In the function set_password_digest we see that we get the password field of the changeset and then update our password_digest with it’s encrypted counterpart:

After this function is done with its task, we have a changeset that we can store.

I want to abstract from the framework as much as possible, so I created a RegisterAction in /lib folder.

As you can see, it just casts the parameters to a user struct and performs validations on it. If is valid, then we return {:ok, user} tuple and if the changeset is invalid, we return an {:error, changeset} tuple. In the first case, we return the user to have the ability to add user.id to set a session in the controller. In the second case, we need to return a changeset to show error messages on the form. The code in controller looks like following:

Please note the line put_session(:user_id, user.id) where we put a user.id in a session. In Phoenix controller actions, you always have a connection or conn for short. This is where the all connection data is stored, including sessions, assigns (We will talk about that later), headers e.t.c.

So, after this we have a session set and how do we provide a user’s context to the application? After a brief research, I decided to go with writing a custom plug and adding current user to conn assigns. Let’s see example:

We import Plug.Conn and Plug.Session to import all functions that can work with sessions and the connection data set itself.

To have a plug that we can use in our controllers, we need to have call implemented with two arguments. First one is the connection and the second one is the options we pass. Since we don’t care about the second argument, we just ignore it.

We try to get user_id from a session user_id = get_session(conn, :user_id). If the session is set, we find given user in the database in line assign(conn, :current_user, ElixirStream.Repo.get(ElixirStream.User, user_id)) and put it in to the connections assigns. This allows us to maintain current user across all the actions.

If we want to use this authentication mechanism and setting of the current_user, we just add this plug to our controller in following way:

Now, each controller action’s conn will or will not contain current user in assings. We can access our assigns like so @conn.assigns[:current_user] which in turn will return nil or a %User{} struct containing logged in user.

So how can we use it in templates to see current user’s avatar or username? The answer is view! In the web/views/layout_view.ex we define the following function:

If we want to get current user in view, we can do it in following way:

This about concludes the brief overview of the registration process of http://elixirstream.com

Conclusions

What I love about Phoenix, is that it gives an ease of mind. All the things that are happening are under your nose and you don’t get lost in all the objects that are flying around. I am looking at you Rails!

Even if documentation sometimes is scarce, it is a great pleasure to write a web app in Phoenix. It took some time to understand all the concepts behind it, but now that I’ve done the full cycle, the next one will be easier.

Love your craft!

P.S You can check the source code for ElixirStream here: https://github.com/janjiss/elixir-stream-phoenix P.S.S I would like to thank my friend Dainis Lapins, for helping me with this app.