Unlock Seamless Authentication: A Step-by-Step Guide to Implementing Gmail Login in Laravel

4 min readNov 18, 2023

In the ever-evolving landscape of web development, providing users with a hassle-free and secure authentication experience is paramount. One way to achieve this is by integrating social login options, and Gmail is a popular choice. This blog will guide you through the process of implementing Gmail login in a Laravel application using Laravel Socialite.

Setting Up Gmail API and Obtaining OAuth 2.0 Credentials in Google Cloud Console:

Before diving into the Laravel code, let’s configure the Gmail API and obtain OAuth 2.0 credentials from the Google Cloud Console.

Navigate to Google Cloud Console:

  1. Visit the Google Cloud Console.
  2. Create a new project or select an existing one.

Step 1: Enable the Gmail API:

  1. In the left sidebar, click on the hamburger icon to open the menu.
  2. Navigate to “APIs & Services” > “Dashboard.”
  3. Click on “+ ENABLE APIS AND SERVICES” at the top.
  4. Search for “Gmail API” in the search bar.
  5. Select the “Gmail API” from the results.
  6. Click the “ENABLE” button to enable the Gmail API for your project.

Step 2: Create OAuth 2.0 Credentials:

  1. In the left sidebar, click on “APIs & Services” > “Credentials.”
  2. Click on “Create Credentials” and choose “OAuth client ID.”
  3. Choose the application type (Web application in this case).
  4. Set the name of your OAuth client ID.
  5. Add the authorized redirect URI. This will be the URL where Google will redirect after authentication. It should be in the format http://your-app-url/callback.
  6. Click “Create.”

Step 3: Obtain Client ID and Client Secret:

  1. Once created, your OAuth client will be listed under “Credentials.”
  2. Click on the edit icon next to your newly created OAuth client.
  3. You will find the “Client ID” and “Client Secret” on the details page. Note these down.

Step 4: Update Laravel Configuration:
Now that you have the OAuth 2.0 credentials, update your Laravel application with this information.

  1. Open your Laravel project’s .env file.
  2. Add the following lines with the obtained client ID and client secret:

Make sure to replace your-client-id, your-client-secret, and your-app-url/callback with the actual values obtained from the Google Cloud Console.

With these OAuth 2.0 credentials, your Laravel application will be able to authenticate users via Gmail. Now, proceed with the Laravel Socialite setup as outlined in the main guide to complete the integration.

Step 5: Install Laravel Socialite Package:
With the Gmail API configured and the OAuth 2.0 credentials obtained, it’s time to integrate Laravel Socialite into your Laravel project.

Open your terminal and run the following command:

composer require laravel/socialite

This command installs the Laravel Socialite package, which simplifies the process of authenticating with third-party services.

Step 6: Configure Gmail Service in Laravel:
Now, let’s update the Laravel configuration to include the Gmail service.

Open your config/services.php file and add the following lines:

'gmail' => [
'client_id' => env('GMAIL_CLIENT_ID'),
'client_secret' => env('GMAIL_CLIENT_SECRET'),
'redirect' => env('GMAIL_REDIRECT'),

These lines tell Laravel where to find your Gmail client ID, client secret, and redirect URL, which are stored in the .env file.

Step 7: Update Environment Variables:
Go back to your .env file and add the Gmail credentials:


Replace your-client-id, your-client-secret, and your-app-url/callback with the actual values you obtained from the Google Cloud Console.

Step 8: Define Routes for Gmail Login:
Open your routes/web.php file and define the routes for Gmail login:

Route::get('auth/gmail', 'Auth\LoginController@redirectToGmail');
Route::get('auth/gmail/callback', 'Auth\LoginController@handleGmailCallback');

These routes specify the URL paths for initiating the Gmail login and handling the callback after authentication.

Step 9: Implement Controller Methods:
In your LoginController, create methods to handle the redirection to Gmail and the callback:

use Socialite;

public function redirectToGmail()
return Socialite::driver('gmail')->redirect();
public function handleGmailCallback()
$user = Socialite::driver('gmail')->user();
// Your authentication logic here

These methods use Laravel Socialite to initiate the OAuth flow and retrieve user information after authentication.

Step 10: Create Gmail Login Button:
Finally, in your login view, add a button to initiate the Gmail login:

{!! Form::open(['url' => 'auth/gmail', 'method' => 'get']) !!}
{!! Form::button('Login with Gmail', ['type' => 'submit']) !!}
{!! Form::close() !!}

This button will redirect users to Gmail for authentication.


Congratulations! You’ve successfully integrated Gmail login into your Laravel application, providing users with a seamless and secure authentication option. This not only enhances user experience but also streamlines the login process.

By following this step-by-step guide, you’ve unlocked the potential of social authentication in Laravel, opening doors to a more user-friendly and interconnected web experience. Experiment with other social platforms using Laravel Socialite to further expand your application’s authentication options.

Remember, user convenience and security should always be at the forefront of your web development endeavors.

Stay tuned for more Laravel tutorials and tips on our blog!

Go forth and get more out of your content. Go forth and conquer Medium! (and while you’re at it, follow me on Medium! and feel free to Subscribe)

Found this post useful? Kindly tap the 👏 button below! :)

🌟 Enjoy my blogging content? Support my work by buying me a virtual coffee on BuyMeACoffee! Your contributions help fuel insightful tech articles. Join hands in making coding more accessible and exciting for all. https://www.buymeacoffee.com/arjunamrutiya 🚀




👋 Hey there! I'm Arjun Amrutiya, a passionate web developer and blogger who loves all things PHP, Laravel and Vue.js. Welcome to my Medium account!