EmberCLI, Torii and Rails
This is the second part to my previous post.
When we left off we had a ember app setup on divshot to talk to a rails application for authentication via github. We’ll start by generating a new rails app:
The first thing we’ll want to do is create the route that our ember application is posting the github temporary token to:
Then we’ll make our sessions controller:
We have to skip the verify_authenticity_token before_action which is used to ensure requests are coming from the same origin. As our ember application will be on a different domain than our rails application, we need this check to be skipped.
The create action is a great example of writing code I wish I had. The first
line creates a new instance of the
GithubAuthenticator class. This class
should be passed the github auth token. The second line passes the instance of
GithubAuthenticator to instantiate another class that doesn’t yet exist, the
UserFactory. We then call
find_or_create_user on the
should return a
user, and pass the result to be rendered as json with a status
:created. We always pass
:created as the status as we’re letting the
browser know that a new
session, not a
user, has been created.
Let’s go ahead and make the
UserFactory class so we can spec out what the
GithubAuthenticator needs to provide:
So we pass an authenticator to the
UserFactory’s initialize. The
find_or_create_user then finds or creates the user using a name provided by
authenticator. There’s nothing here specific to github, so adding extra
sign in services is just a case of creating other authenticator classes that
Now let’s look at the
This class is pretty long, and I’d consider extracting a class to handle the
oauth post to github. I won’t go over every line, as I hope the code is clear,
but here’s an overview. We initialize with the temporary token provided by
github and make a
Net::HTTP request following the guidelines provided by
github. This returns an oauth token. We
use Octokit to request the user from
github and extract the
:login for our
We’ll need to add
gem "octokit" to our gemfile and run
bundle. I also use
dotenv to manage environment variables in development so go ahead and add
test and put your github authentication details into
Finally we’ll want to create a
User model and a migration:
The user class is simple enough, we generate a unique token on create that the ember app can use for retrieving a session.
Now if we have everything wired up correctly, with our rails app running:
and our ember-cli app running:
we should be able to visit localhost:4200 and click on our login button. We then see our success alert and can see in the console the rails app returning our user:
Now lets get this up on heroku.
Get yourself an account on heroku and install the toolbelt with
heroku-toolbelt. Once you’re setup, go ahead and create your heroku app. You’ll
then want to commit all our changes to git (if you haven’t already) and push
your code up to heroku:
We’ll also want to set our two environment variables for github credentials:
That gets us the same functionality in production!
Next time we’ll setup creating a session on the ember side and allowing the ember app to retrieve the session using the user’s token.