Reader Stacks

Social Login and Social Sharing in Laravel: Google, Facebook, and Share Buttons

Laravel Socialite handles both Google and Facebook login with the same driver-based API — sharing a page out to social media, by contrast, needs no package at all.

Social Login and Social Sharing in Laravel: Google, Facebook, and Share Buttons

Social login (letting users sign in with an existing Google or Facebook account) and social sharing (letting visitors share your content out to those platforms) are two genuinely different features that only sound similar — Laravel Socialite handles the first, and the second needs no package at all.

Installing Socialite

composer require laravel/socialite

Configuring Google login

// config/services.php
'google' => [
    'client_id' => env('GOOGLE_CLIENT_ID'),
    'client_secret' => env('GOOGLE_CLIENT_SECRET'),
    'redirect' => env('GOOGLE_REDIRECT_URI'),
],
Route::get('/auth/google/redirect', function () {
    return Socialite::driver('google')->redirect();
});

Route::get('/auth/google/callback', function () {
    $googleUser = Socialite::driver('google')->user();

    $user = User::updateOrCreate(
        ['email' => $googleUser->email],
        ['name' => $googleUser->name, 'google_id' => $googleUser->id]
    );

    Auth::login($user);

    return redirect('/dashboard');
});

Configuring Facebook login

// config/services.php
'facebook' => [
    'client_id' => env('FACEBOOK_CLIENT_ID'),
    'client_secret' => env('FACEBOOK_CLIENT_SECRET'),
    'redirect' => env('FACEBOOK_REDIRECT_URI'),
],
Route::get('/auth/facebook/redirect', function () {
    return Socialite::driver('facebook')->redirect();
});

Route::get('/auth/facebook/callback', function () {
    $fbUser = Socialite::driver('facebook')->user();

    $user = User::updateOrCreate(
        ['email' => $fbUser->email],
        ['name' => $fbUser->name, 'facebook_id' => $fbUser->id]
    );

    Auth::login($user);

    return redirect('/dashboard');
});

Both providers follow the identical Socialite API — driver('provider')->redirect() then driver('provider')->user() in the callback — which is exactly the point of using Socialite rather than implementing each provider's OAuth flow by hand.

Handling an email that already exists under a different provider

$user = User::where('email', $googleUser->email)->first();

if ($user && ! $user->google_id) {
    $user->update(['google_id' => $googleUser->id]);
} elseif (! $user) {
    $user = User::create([...]);
}

Auth::login($user);

Checking for an existing email before blindly creating a new user avoids ending up with duplicate accounts when someone who originally registered with a password later tries logging in via Google using the same email address.

Sharing a page to Facebook


    Share on Facebook

Sharing to WhatsApp


    Share on WhatsApp

Why sharing needs no package or API credentials

Both share links above are just plain URLs with query parameters — no OAuth, no API key, no server-side code — the platforms handle rendering their own share dialog once the visitor clicks through, which is why this is dramatically simpler to implement than the login flows above despite involving the same two platforms.

A generic multi-platform share package as an alternative

For supporting many platforms at once with consistent markup, a small package that generates the correct share URL per platform from one Blade component is a reasonable convenience — though given how simple each individual share link is, hand-rolling them (as above) is equally reasonable and avoids an extra dependency for something this small.