Introduction:
In today’s digital world, social login integration has become a common feature in web applications. It provides users with a convenient way to sign in using their existing social media accounts, reducing the hassle of creating and remembering yet another password. Laravel, a popular PHP framework, simplifies this integration process with its Socialite package, allowing developers to easily implement social logins. In this guide, we’ll walk through the steps to integrate Google Account login using Laravel 11 Socialite.
Step 1:
Set Up Your Laravel Project Firstly, ensure you have Laravel 11 installed on your system. If not, you can install it via Composer using the following command:
composer create-project --prefer-dist laravel/laravel myproject
Navigate to your project directory:
cd myproject
Step 2:
Configure Google Developer Console To enable Google Account login, you need to create a project in the Google Developer Console and obtain OAuth 2.0 credentials. Follow these steps:
- Go to the Google Developer Console (https://console.developers.google.com/).
- Create a new project and give it a name.
- In the sidebar, navigate to “Credentials” and click on “Create credentials” -> “OAuth client ID”.
- Choose “Web application” as the application type.
- Set the authorized redirect URI to
http://your-domain.com/auth/google/callback
(replaceyour-domain.com
with your actual domain). - Click “Create” to generate your OAuth client ID and client secret.
Step 3:
Install Laravel Socialite Laravel Socialite simplifies OAuth authentication with various providers, including Google. Install it via Composer:
composer require laravel/socialite
Step 4:
Configure Laravel Socialite Open your .env
file and add your Google OAuth credentials:
GOOGLE_CLIENT_ID=your-client-id
GOOGLE_CLIENT_SECRET=your-client-secret
GOOGLE_REDIRECT=http://your-domain.com/auth/google/callback
Step 5:
Implement Google Account Login Now, let’s create the necessary routes and controller methods to handle Google Account login:
- Define routes in
routes/web.php
:
use App\Http\Controllers\Auth\GoogleController;
Route::get('auth/google', [GoogleController::class, 'redirectToGoogle']);
Route::get('auth/google/callback', [GoogleController::class, 'handleGoogleCallback']);
- Create a controller named
GoogleController
using the artisan command:
php artisan make:controller Auth/GoogleController
- Implement the controller methods:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
class GoogleController extends Controller
{
public function redirectToGoogle()
{
return Socialite::driver('google')->redirect();
}
public function handleGoogleCallback()
{
$user = Socialite::driver('google')->user();
// Use $user data to authenticate user or register new user
return redirect()->route('home'); // Redirect to home page after login
}
}
Step 6:
Create Views (Optional) You can create login buttons and views for your application as needed. For example, you might create a login.blade.php
file with a button that redirects users to /auth/google
.
Step 7:
Test Your Implementation Run your Laravel application using the php artisan serve
command and navigate to the login page. Click the Google login button, and you should be redirected to Google’s authentication page. After granting permission, you’ll be redirected back to your application, logged in with your Google Account.
Conclusion:
Integrating Google Account login with Laravel 11 Socialite is a straightforward process, thanks to Laravel’s built-in support for OAuth authentication. By following the steps outlined in this guide, you can enhance the user experience of your Laravel application by providing a seamless social login option.
- Check our tools small Tools
- Check our tools website Word count