Implementing Social Media Login in Laravel (Google, Facebook, GitHub Auth)

Implementing Social Media Login in Laravel (Google, Facebook, GitHub Auth)

Introduction

Social media login allows users to authenticate using third-party services like Google, Facebook, and GitHub. In this guide, we will implement social login in Laravel using Laravel Socialite.

1. Installing Laravel Socialite

To get started, install the Socialite package:

composer require laravel/socialite

2. Configuring Socialite

In config/services.php, add credentials for each provider:

return [
    'google' => [
        'client_id' => env('GOOGLE_CLIENT_ID'),
        'client_secret' => env('GOOGLE_CLIENT_SECRET'),
        'redirect' => env('GOOGLE_REDIRECT_URI'),
    ],
    'facebook' => [
        'client_id' => env('FACEBOOK_CLIENT_ID'),
        'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
        'redirect' => env('FACEBOOK_REDIRECT_URI'),
    ],
    'github' => [
        'client_id' => env('GITHUB_CLIENT_ID'),
        'client_secret' => env('GITHUB_CLIENT_SECRET'),
        'redirect' => env('GITHUB_REDIRECT_URI'),
    ],
];

3. Setting Up Environment Variables

Add your API credentials in the .env file:

GOOGLE_CLIENT_ID=your-google-client-id
GOOGLE_CLIENT_SECRET=your-google-client-secret
GOOGLE_REDIRECT_URI=http://localhost:8000/auth/google/callback

FACEBOOK_CLIENT_ID=your-facebook-client-id
FACEBOOK_CLIENT_SECRET=your-facebook-client-secret
FACEBOOK_REDIRECT_URI=http://localhost:8000/auth/facebook/callback

GITHUB_CLIENT_ID=your-github-client-id
GITHUB_CLIENT_SECRET=your-github-client-secret
GITHUB_REDIRECT_URI=http://localhost:8000/auth/github/callback

4. Creating Authentication Routes

Add routes in routes/web.php:

use Laravel\Socialite\Facades\Socialite;
use App\Models\User;
use Illuminate\Support\Facades\Auth;

Route::get('auth/{provider}', function ($provider) {
    return Socialite::driver($provider)->redirect();
});

Route::get('auth/{provider}/callback', function ($provider) {
    $user = Socialite::driver($provider)->user();
    $existingUser = User::where('email', $user->getEmail())->first();
    if ($existingUser) {
        Auth::login($existingUser);
    } else {
        $newUser = User::create([
            'name' => $user->getName(),
            'email' => $user->getEmail(),
            'provider_id' => $user->getId(),
            'provider' => $provider
        ]);
        Auth::login($newUser);
    }
    return redirect('/home');
});

5. Updating the User Model

Modify the User model to include provider fields:

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    protected $fillable = [
        'name', 'email', 'provider', 'provider_id'
    ];
}

6. Adding Login Buttons in Blade Template

Add social login buttons to your login page:

<a href="{{ url('auth/google') }}">Login with Google</a>
<a href="{{ url('auth/facebook') }}">Login with Facebook</a>
<a href="{{ url('auth/github') }}">Login with GitHub</a>

Conclusion

By following these steps, you can integrate social media login in Laravel using Google, Facebook, and GitHub authentication, making the authentication process seamless for users.

© 2025 Developer Sahayak. All Rights Reserved.

0 Comments