In the previous part of this series, we created our initial interfaces which weâ€™ll be using for all future parts. In this article, we will integrate Google+ within our application.
You can view the code for all the articles on this Github page.
We start off by creating the following directory: src/SitePoint/SocialLogin/Google. This will be the directory we will be working in for the rest of the article. Within this directory, we create the GoogleLogin class. This class implements the SocialLoginInterface interface which we created in the previous article. Make sure a property called service is available to store our service in (see code below).
Since all 3 properties are required for our application to work, our constructor will receive these 3 variables as parameters and set the properties.
Until now, our code looks like this.
The next step is to create our Google+ service. As you have seen in the previous article, we are using an OAuth library to set up our connection with Google+. We will be using our init method to create the service.
Before we can start, we have to add some use statements to the top of our class.
Everything for our init method is now present. Time to set up our connection with Google+. We do this with the basic examples of the OAuth library we are using.
Our service is set now, so we can continue by filling out other methods from the interface. We will start off with the getLoginUrl method. This method will return a URL which you will be redirecting your user to.
The next method to define is the loginCallback method. When a user accepts Google+ sharing data with your application, the user will be redirected back to the url you defined as the callbackUrl. When the user is requesting that URL, you should call the loginCallback method from this class.
If everything went well, we can now retrieve data from Google+.
You might notice we are using a class named GoogleUser here and we are returning a class with the interface SocialUserInterface. However, this class is not yet present.
Every social network returns user data in a different way. Instead of just returning this data, we are going to normalize it so we can easily work with it. We need to create a class named GoogleUser. This class will implement the SocialUserInterface we created in the previous article.
The constructor of this class expects the raw data of Google in this case. In every getter, we will retrieve the data we want from the raw data from Google. In the end, your GoogleUser class could look like this.
Depending on the framework you are using (if you are using any of course) the implementation can be slightly different. However, I would recommend you have methods like the following below.
//Renders a login form with a button to login with google+ (calls the LoginWithGoogle method).
// Redirect user to $googleUrl.
// You can now either save or merge this user.
Instead of a link in your log in form which redirects you to the loginWithGoogle method, you could also place the Google URL into your view. However, note that you are already creating a connection with Google. This could slow down your page. Especially when using multiple social networks. In general, I would recommend the way above.
If you are going to use multiple social logins, you could easily add a parameter named $socialNetwork to the loginWith and the loginCallback method and add a switch statement inside.
If you want to run a quick test to try out the code, clone the Github repository to your local computer and switch to the branch named part2. Fill in your Google+ API details within the testGoogle.php file and run the file from within your browser. When requesting the page, you will be redirected to a Google page, requesting you to share information.
When clicking accept, you will be redirected back to the page you configured as callback URL, showing your first and last name.
In this article we took the base we set in the previous article and worked further on that. We implemented a way to log in with Google+ and made sure we returned an object which normalizes the data you received from Google. In the upcoming article, we will have a look at how we can merge accounts when people are using multiple logins. In follow up articles we will take a look at how we add more social networks to our system.