Google+ is improving every day, leading to more developers creating apps with support for the social network. Recently there seems to be a build-up of hype regarding the Google+ API. Google’s documentation is not bad, but since authentication is performed on the client side, there isn’t much support for how to integrate this into the back-end of an application. This tutorial shows the way I use Google+ to authenticate users in Ruby on Rails.
Don’t forget to run
bundle install to install your gems! Next, generate a resource called “user”. This will create a controller, model, and views folder to handle the module. Default routes will also be added in the routes config file. We’re going to pass a few parameters to this command as well:
This command will generate a resource called “user” with parameters called “name” and “gplus”. This will create a database schema for us: a table called “users” with columns “id”, “name”, and “gplus”. We can then create this database by running:
Congratulations! You just created the skeleton for our app!
We’ll begin our code in the model. Open up /googlePlusApp/app/models/users.rb and insert this code:
Even with minimal Ruby knowledge, this block of code is pretty easy to understand. Our accessible attributes are “name” and “gplus”. We validate the presence of “name” and “gplus”, as well as validate the uniqueness of “gplus”. This is to be sure that a Google+ user does not authenticate themselves more than once. Just like that, our model is done!
Next, we’re going to move on to the view to work on the client-side validation. Create a file in /googlePlusApp/app/views/users and name it “login.html.erb”. First, we’re going to want to add a Google+ sign-in button. According to the Google+ API manual, we can do that with this code:
Where “CLIENT_ID” is the client ID you are given when registering your app through the Google API console. Notice the callback function named “signinCallback”? This will allow us to get the profile data from Google via ajax after the button is clicked and the user has authorized. Before doing this, we need to create a form with hidden fields to send over to our controller later:
“is-this-you” will be used to display the user’s name and image after we get the data via ajax. In order to do this, let’s include the Google+ API at the top of our page.
Now it’s time to write our callback function “signinCallback”. Let’s go ahead and add the whole function first inside our script tags, then I’ll explain the details:
There’s a lot going on here! Let’s go through it step by step.
Here we define our access token variable. This is a temporary token that is needed in the authorization URL.
If we are successful in getting our access token, we begin our main callback function. We also define the authorization URL given by Google with the addition of our access token at the end.
This starts our ajax request to get the profile data from Google. This is a typical “GET” HTTP request at the URL we defined previously. This returns a JSON object.
This starts our success function if the ajax request is successful. This part here may seem a bit confusing. When Google+ returns the profile image, they specify a size of 50px (it’s a square image). This is noted by the “?sz=50” at the end of the image URL. To make the image a little bit larger, I slice the last two characters of the URL and replace it with “200”. This will give our image a size of 200px so it is easier to see.
This is the end of our success function, but the most important part! Here we fill the hidden fields we created earlier with profile data. The “fullname” field is populated with the user’s display name and the “gplus” field is populated with the user’s profile ID. For added spice and security, we also display the user’s name and profile image inside the “is-this-you” div so they can double check that they are signing in with the right profile! Finally, if the ajax request is unsuccessful, we log an error message in the console.
The end of our callback function that displays an error message on the page if Google+ authentication fails. This is all you need on the client side–now it’s time to validate this data in the back-end of our app.
Head on over to /googlePlusApp/app/controllers/users_controller.rb. We’re going to need a few methods here. First:
This is a simple method defining “login” that will allow us to open a login view.
This method does multiple things. First, it checks if the user already exists in our system. We try to find the user by their Google+ ID. If there is already a user registered with the specified Google+ ID, the page redirects to the root URL and shows a notice telling the user they have already registered. If the user doesn’t exist in our system, it creates a new user. It fills in the fields in our database with the user’s name and Google+ ID that we sent over from the login form. Once the user is saved, the page redirects to the root URL and shows a notice telling the user they have been successfully registered with their name and ID displayed.
And there you have it! This is a very simple authentication system using the Google+ API for Ruby on Rails. The profile ID acts as the user’s password in this sense–since our system can only log in users after they have validated on the client side through Google+, we can skip the usual username/password login. Of course, extra security should be considered if you are planning to launch a live application!
Feel free to leave a comment if you have any questions or suggestions!