Share this:

Hello dev, Today we are going to learn Installing Socialite Package in Laravel 10. Laravel, known for its simplicity and elegant syntax, has become the go-to PHP framework for web developers worldwide.

One of the reasons for its popularity is the extensive collection of packages available to extend its functionality. Socialite, a popular Laravel package, simplifies the integration of third-party OAuth providers, allowing users to authenticate via their social media accounts.

In this blog, we will walk you through the process of installing Socialite package in Laravel 10 and integrating it into your application, enabling seamless social media authentication.

Steps on Installing Socialite Package in Laravel 10

Below are the steps on how to install the Socialite package in Laravel 10:

Also Read: Building a Laravel 10 CRUD Application: A Comprehensive Tutorial

Step 1: Set Up a New Laravel 10 Project

Before we can install Socialite, we need a Laravel 10 project up and running. If you don’t have Laravel installed, follow these steps:

  • Open your terminal or command prompt.
  • Run the following command to create a new Laravel project:
composer create-project laravel/laravel your-project-name

Replace “your-project-name” with the desired name for your project.

  • Navigate to the project directory:
cd your-project-name

Step 2: Install Socialite Package

To install Socialite, use Composer, which is Laravel’s package manager:

Run the following command in your terminal:

composer require laravel/socialite

Composer will download and install the required Socialite package and its dependencies.

Also Read: How to Get Client IP Address in Laravel 10

Step 3: Configure Socialite

Once Socialite is installed, you need to configure it to work with your application and the OAuth providers you want to use. Open the config/services.php file and add the following lines to the providers array:

'socialite' => [
    'github' => [
        'client_id' => env('GITHUB_CLIENT_ID'),
        'client_secret' => env('GITHUB_CLIENT_SECRET'),
        'redirect' => env('GITHUB_REDIRECT_URL'),
    ],
    // Add more providers here if needed
],

Step 4: Obtain OAuth Credentials

For Socialite to work correctly, you’ll need OAuth credentials from the social media platforms you wish to integrate. For example, if you want to enable GitHub authentication, you’ll need a GitHub OAuth application.

  • Go to the respective social media platform’s developer portal (e.g., GitHub Developer Settings for GitHub).
  • Create a new OAuth application, and you’ll receive a client ID and a client secret.
  • Set the appropriate redirect URL (usually, http://your-app-url/login/callback).

Step 5: Set Environment Variables

Now, add the obtained OAuth credentials to your Laravel application’s .env file:

GITHUB_CLIENT_ID=your_github_client_id
GITHUB_CLIENT_SECRET=your_github_client_secret
GITHUB_REDIRECT_URL=http://your-app-url/login/callback

Make sure to replace your_github_client_id, your_github_client_secret, and http://your-app-url/login/callback with the actual values.

Also Read: Bulk Insertion in Laravel 10 using eloquent ORM

Step 6: Implement Social Login in Your Application

With Socialite fully configured, it’s time to implement social login in your Laravel 10 application. For this example, we’ll implement GitHub authentication:

  • Create a new route in routes/web.php for handling the GitHub login:
Route::get('/login/github', 'AuthController@redirectToGithub')->name('github.login');
Route::get('/login/callback/github', 'AuthController@handleGithubCallback');
  • Create a new controller using the following command:
php artisan make:controller AuthController
  • In the AuthController controller, add the following methods:
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

public function redirectToGithub()
{
    return Socialite::driver('github')->redirect();
}

public function handleGithubCallback()
{
    $user = Socialite::driver('github')->user();

    // Implement your logic to log in the user or register the user as needed
    // For example, you can use the $user data to retrieve user details and perform actions accordingly.

    Auth::login($user);

    return redirect()->intended('/home');
}

Step 7: Implement Logout Functionality

To log out a user, you can use Laravel’s built-in Auth facade. Add a route for logout in your routes/web.php file:

Route::post('/logout', 'AuthController@logout')->name('logout');

Then, add the following method to your AuthController:

public function logout()
{
    Auth::logout();
    return redirect('/');
}

Conclusion:

Congratulations! You’ve successfully installed the Socialite package in Laravel 10 and implemented social media authentication using GitHub as an example. You can now extend this functionality to integrate with other social media platforms by following similar steps. Socialite significantly simplifies the OAuth integration process, making it a valuable addition to your Laravel applications.

Remember to always prioritize the security of your application and follow best practices while integrating third-party authentication. With Socialite, you can enhance your users’ experience by allowing them to seamlessly log in using their preferred social media accounts. Happy coding!

Share this:

Categorized in: