Adding Google Authentication to Laravel 8 Website

Kenpachi Zaraki
5 min readJun 8, 2021

With social authentication, users do not have to fill out forms in order to register on your website; they also do not need to remember another password in other to access their accounts on your website. This article will provide a step-by-step process to add Google authentication to your website

Assumptions:

This tutorial assumes that you have PHP and Composer installed on your computer. It also uses XAMPP’s Apache and MySQL installations. It is therefore assumed that the reader is familiar with XAMPP. Visit the Composer and XAMPP websites to download them.

1. Create a new Laravel project

Run the following on your command line to create a new Laravel project called social-auth

composer create-project laravel/laravel social-auth

2. Configure your database setup

You can do this in two steps:

  1. Create a new database called “social-auth” in phpMyAdmin
  2. Edit your .env file with the appropriate details as shown below

3. Setup your basic authentication system with Laravel Breeze

Laravel 8 provides three free authentication starter packages: Laravel Breeze, Laravel Jetstream, and Laravel Fortify. For this tutorial, we will be using Laravel Breeze. Feel free to use whichever you want. It should not make a difference for what we need to do here.

To setup Laravel Breeze, run the following lines in the order they appear on your command line:

composer require laravel/breeze --devphp artisan breeze:installnpm installnpm run devphp artisan migrate

4. Install Laravel Socialite and add providers and aliases in your config file

Run the following line on your command line to install Laravel Socialite

composer require laravel/socialite

In your config/app.php, add Socialite to your providers and aliases arrays thus:

....
....
'providers' => [
....
....
Laravel\Socialite\SocialiteServiceProvider::class,
],
'aliases' => [
....
....
'Socialite' => Laravel\Socialite\Facades\Socialite::class,
],
....
....

5. Create a new Google app

Follow this link to create a new Google app. It will take you to a page that looks like the one shown below if you are doing this for the first time :

If you have done something like this before, the page will look something like this. The rest of the tutorial is based on the assumption that you are doing this for the first time.

When on the page do the following:

  1. Click the “CREATE PROJECT” link. Provide required details for the resulting page.

2. Click the Credentials option on the sidebar

3. Click the “CREATE CREDENTIALS” item on the navbar and select the “OAuth Client ID” option from the resulting dropdown widget.

4. Click the “CONFIGURE CONSENT SCREEN” button.

5. For this case, I’ll pick the “External” option. Feel free to pick whichever works for you.

6. Fill the next form with the appropriate data and click the “SAVE AND CONTINUE” button

7. Click the “ADD OR REMOVE SCOPES” button to add permissions users should authorize in the login process.

After picking your scopes, scroll down to the bottom of the page and click the “UPDATE” button.

8. Add test users on the next page using their email

On the summary page that appears after this one, click the “RETURN TO DASHBOARD” button.

9. Go back to the credentials page and create new credentials as explained in step 3.

10. Provide details as shown in the screenshots below and click the “CREATE” button.

Add a Google entry to your config/services.php file.

return [
....
....
....
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET',
'redirect' => env('GOOGLE_CLIENT_REDIERECT',
],
]

Add the following to your .env file

GOOGLE_CLIENT_ID="Your Google app id"
GOOGLE_CLIENT_SECRET="Your Google client secret"
GOOGLE_CLIENT_REDIERECT="http://localhost:8000/auth/google/callback"

6. Add a“google_id” column to your “users” table

Run the following line in your command line to create the migration file to add a google_id column the users table

php artisan make:migration add_google_id_to_users_table

Make the following changes to the file created.

Run the following line on your command line to update your database

php artisan migrate:fresh

7. Setup your Google authentication routes

Add the following lines to your routes/web.php file.

use App\Http\Controllers\SocialController;//Google Authentication RoutesRoute::get('auth/google', [SocialController::class, 'googleRedirect']);Route::get('auth/google/callback', [SocialController::class, 'googleLoginOrRegister']);

8. Create and configure controller to handle the routes

Run the following line on your command line to create a controller to handle social authentication.

php artisan make:controller SocialController

Define googleRedirect() and googleCallback() as shown below:

9. Add Google login button to your registration and login pages

Add a copy of the link below to both the register.blade.php and login.blade.php in the resources\views\auth folder

<div class="flex items-center justify-center mt-4">
<a href="{{ route('login.google') }}" class="underline text-sm text-gray-600 hover:text-gray-900">
Login With Google
</a>
</div>

--

--