Tuesday, March 12, 2024

How to Create Google Login Page in Laravel 11


 This article will guide you through the steps to set up Google login functionality for your Laravel 11 web application. Integrating Google login provides an easy way for users to access your application using their existing Google accounts.

Laravel 11, a highly popular PHP framework for web application development, often requires Google login features. In this article, we will explain how to implement this feature.

Step 1: Laravel 11 Installation

Begin by installing Laravel 11 using the following command in the terminal:

composer create-project laravel/laravel my-google-login-app "11.*"

Once done, navigate to the project directory with the command:

cd my-google-login-app

Step 2: Configure Google OAuth

Next, set up Google OAuth for your Laravel application. Open the .env file and add your Google OAuth information like this:

GOOGLE_CLIENT_ID=your-client-id

GOOGLE_CLIENT_SECRET=your-client-secret

GOOGLE_REDIRECT_URI=http://localhost:8000/auth/google/callback

You can obtain CLIENT_ID and CLIENT_SECRET by creating a project in the Google Developer Console.

Step 3: Install Laravel Socialite

Laravel Socialite simplifies integration with various OAuth services, including Google. Install this package with the command:

composer require laravel/socialite

Step 4: Configure Laravel Socialite

Add Laravel Socialite configuration by opening the config/services.php file and adding the following lines:

'google' => [

    'client_id' => env('GOOGLE_CLIENT_ID'),

    'client_secret' => env('GOOGLE_CLIENT_SECRET'),

    'redirect' => env('GOOGLE_REDIRECT_URI'),

],

Step 5: Route and Controller Creation

Create routes for Google login and a controller to handle authentication logic. Add the following routes to the routes/web.php file:

Route::get('/auth/google', 'Auth\GoogleController@redirectToGoogle');

Route::get('/auth/google/callback', 'Auth\GoogleController@handleGoogleCallback');

Next, create the controller with the command:

php artisan make:controller Auth/GoogleController

Step 6: Implement Authentication Logic

Open the newly created controller (Auth/GoogleController.php) and add Google authentication logic.

use Socialite;

use Illuminate\Support\Facades\Auth;


public function redirectToGoogle()

{

    return Socialite::driver('google')->redirect();

}


public function handleGoogleCallback()

{

    $user = Socialite::driver('google')->user();

    // Add logic to save or authenticate the user

    // Example: $user->getEmail(), $user->getName()

    // Authenticate the user if necessary

    Auth::login($user);

    return redirect('/home'); // Adjust to the desired page

}

Step 7: Google Login View

Finally, create a view that allows users to log in with Google. You can integrate it with Laravel's default login view or create a custom one.

By following the above steps, you can implement Google account login features in your Laravel 10 application, providing users with the convenience of signing in using their Google accounts. We hope this article proves useful in your web application development.

0 comments:

Post a Comment