Laravel 5.2 Socialite Google Login (Part 3)

Before trying to merge multiple social accounts in a single one, let's add one more option for users to log in. This time with google. This will be easiest so far because everything is prepared in the previous tutorials. Thing that will explain is how to get client_id and client_secret for our configuration.

Getting Credentials

On this page you can find instructions. I will try to simplify this.

Open Developers Console and create New Project.

Creating new project

Type app name

Enter app name

Wait few seconds for app to be ready. Then expand left menu and click on API Manager.

API Manager in sidebar

Select Credentials submenu and then chose OAuth consent screen tab.

OAuth consent screen

Fill in Product name shown to users field and save.

Click New credentials and OAuth client ID.

Creating new credentials

Now we will define our client as a client. Fill in name. We will authorize our development url which is http://localhost:8000 and we will use like before callback route with google parameter: http://localhost:8000/callback/google.

Creating client

One more thing before proceeding. We need to enable Google+ API. Click on the Overview tab on the left side then Google+ API. On the new page click Enable API.

Google+ API

Configuring Socialite

And finally, we have credentials. We can build configuration for Socialite package. Copy configuration in the config/services.php.

'google' => [
    'client_id' => '',
    'client_secret' => '23zeZz1ya9syFMH4ggQM2e-p',
    'redirect' => 'http://localhost:8000/callback/google',

Add link in the resources/views/auth/login.blade.php view. <a href="redirect/google">G+</a>. Now when user clicks on the link it will be redirected to the google's page an asked for permission. After that user will be redirected to our app and successfully logged in.

Occasionally it happens that google require some time to apply client configuration If you get an error message redirect_uri_missmatch wait couple of minutes and it should work normally.

Uri mismatch


I plan to write more articles about common laravel components. If you are interested let’s stay in touch.
comments powered by Disqus