The Universal Bookshelf platform supports social login through OAuth 2.0 for Google and Facebook. This allows users to authenticate using their existing social media accounts, providing a seamless login experience.
http://localhost:8000/auth/google/callback (for local development)https://yourdomain.com/auth/google/callback (for production)Add the following to your .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
Install Laravel Socialite:
composer require laravel/socialite
http://localhost:8000/auth/facebook/callback (for local development)https://yourdomain.com/auth/facebook/callback (for production)Add the following to your .env file:
FACEBOOK_CLIENT_ID=your_facebook_app_id
FACEBOOK_CLIENT_SECRET=your_facebook_app_secret
FACEBOOK_REDIRECT_URI=http://localhost:8000/auth/facebook/callback
Add the following configuration to your services config file:
'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'),
],
Redirect user to Google OAuth consent screen
This endpoint initiates the Google OAuth flow by redirecting the user to Google's consent screen where they can authorize the application to access their basic profile information.
Redirects to Google OAuth consent screen
Handle Google OAuth callback
This endpoint processes the callback from Google after successful authorization. It creates or updates the user account and logs them in.
code - Authorization code from Googlestate - CSRF protection state parameterRedirects to dashboard or specified redirect URL
Redirect user to Facebook OAuth consent screen
This endpoint initiates the Facebook OAuth flow by redirecting the user to Facebook's consent screen.
Redirects to Facebook OAuth consent screen
Handle Facebook OAuth callback
This endpoint processes the callback from Facebook after successful authorization.
code - Authorization code from Facebookstate - CSRF protection state parameterHere's an example of how to implement the social login controller:
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
use Laravel\Socialite\Facades\Socialite;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class SocialAuthController extends Controller
{
public function redirectToGoogle()
{
return Socialite::driver('google')->redirect();
}
public function handleGoogleCallback()
{
try {
$user = Socialite::driver('google')->user();
$finduser = User::where('google_id', $user->id)->first();
if ($finduser) {
Auth::login($finduser);
return redirect('/dashboard');
} else {
$newUser = User::create([
'name' => $user->name,
'email' => $user->email,
'google_id' => $user->id,
'password' => Hash::make(Str::random(24)),
'email_verified_at' => now(),
]);
Auth::login($newUser);
return redirect('/dashboard');
}
} catch (\Exception $e) {
return redirect('/auth/google')->with('error', 'Google login failed');
}
}
public function redirectToFacebook()
{
return Socialite::driver('facebook')->redirect();
}
public function handleFacebookCallback()
{
try {
$user = Socialite::driver('facebook')->user();
$finduser = User::where('facebook_id', $user->id)->first();
if ($finduser) {
Auth::login($finduser);
return redirect('/dashboard');
} else {
$newUser = User::create([
'name' => $user->name,
'email' => $user->email,
'facebook_id' => $user->id,
'password' => Hash::make(Str::random(24)),
'email_verified_at' => now(),
]);
Auth::login($newUser);
return redirect('/dashboard');
}
} catch (\Exception $e) {
return redirect('/auth/facebook')->with('error', 'Facebook login failed');
}
}
}
You'll need to add social login fields to your users table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('google_id')->nullable()->unique();
$table->string('facebook_id')->nullable()->unique();
$table->string('avatar')->nullable();
});
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn(['google_id', 'facebook_id', 'avatar']);
});
}
};
Add social login buttons to your login form:
<div class="social-login-buttons">
<a href="/auth/google" class="btn btn-danger">
<i class="fab fa-google me-2"></i>Login with Google
</a>
<a href="/auth/facebook" class="btn btn-primary">
<i class="fab fa-facebook me-2"></i>Login with Facebook
</a>
</div>
For additional support or questions about social login implementation, please contact our development team.
Back to Documentation