Sunday, 29 January 2023

Laravel Socialite with Google Account

Using Google Social Login in Laravel is a great way to make it easier for users to log in and make their experience better. Users can log in with just one click using their Google account instead of making new accounts and remembering passwords.

Laravel has an official package called Laravel Socialite that makes it very easy to add social login providers like Google, Facebook, GitHub, and Twitter.

This guide will show you step-by-step how to set up Google Login in Laravel using Socialite. It will cover making a Google app, getting credentials, and setting up Laravel.

Why Use Google Social Login in Laravel?

Before diving into the implementation, let's understand why developers prefer Google login.

Benefits

Quick sign-up process—users can log in right away.
Better security—Google takes care of authentication.
A better experience for users because they don't have to remember passwords.
Google accounts are a common way to verify identity.
Simple to connect with Laravel Socialite
Laravel Socialite makes OAuth authentication easier by taking care of API requests and token management on its own.

Prerequisites

Before you set up Google login in Laravel, make sure you have the following:
Laravel is set up
Installed composer
Account with Google
Getting into Google Cloud Console

Step 1: Install Laravel Socialite

First, install the Laravel Socialite package using Composer.

composer require laravel/socialite

If you're using Laravel 8 or higher, Laravel will automatically register the package after you install it.

Step 2: Create Google App

To make a Google client ID and client secret and add a Google callback, we need to go to

You need to make an app in the Google Cloud Console so that people can log in with Google.

Follow these steps:

Launch the Google Cloud Console.

Start a new project or choose one that already exists.

Go to APIs and Services.

On the left side, click on Credentials.

You can make the API keys, OAuth credentials, and access tokens you need for authentication in this part.

Google Developer Console



Step 3: Generate Google OAuth Credentials

On the credentials page, do the following:

At the top of the page, click on Create Credentials.

Choose the OAuth Client ID.

From the dropdown menu for application types, choose "Web Application."

Click on "credentials" in the left sidebar, and the page will look like the one below.


On top of the page, click on "Create credentials as shown below."


Select a web application from the drop-down list



Enter the application name and the callback redirect URL


After saving the details client_id and secret_id will get generated




Step 4: Configure Google Credentials in Laravel config/services.php. 

Now that you have made your Google credentials, you need to set them up in your Laravel app.

Please open the following file:

config/services.php

For instance,

return [

    'google' => [

        'client_id' => 'your-app-id',

        'client_secret' => 'your-secret-key',

        'redirect' => 'http://localhost:8000/auth/google/callback',

    ],

];

This configuration tells Laravel how to connect with Google's authentication servers.

Step 5: Use Environment Variables (.env)

For security reasons, it's best to keep sensitive information in the .env file instead of hardcoding it.

In the .env file, define all three of the keys above.

Please add the following Google settings here, like the ones below.

GOOGLE_CLIENT_ID=your-client-id
YOUR_SECRET_KEY=GOOGLE_CLIENT_SECRET
GOOGLE_REDIRECT=http://localhost:8000/auth/google/callback

In conclusion: It's easy to add Google Social Login to Laravel with Laravel Socialite. makes the authentication process much better for users. You can do the following by following these steps:

  • Make an app for Google OAuth
  • Make credentials for clients
  • Set up Laravel Socialite
  • Use Google accounts to verify users
Social login is now a common feature of modern web apps, and It's very easy to use Laravel. If you are building SaaS platforms, marketplaces, or any user-focused web application, integrating Google authentication can significantly increase signup rates and reduce friction during user registration.

No comments:

Post a Comment

Laravel DomPDF Package – Generate PDF in Laravel

Understanding the Package One of the most popular tools that developers use to make PDF files directly from HTML views is the Laravel DomPDF...