Installation
composer require birko/minishop-google
Publish the package configuration (if needed):
php artisan vendor:publish --provider="Birko\MiniShopGoogle\MiniShopGoogleServiceProvider"
Configuration
Edit config/minishop-google.php to include:
profile, email, openid)First Use Case: OAuth Login
Add the Google login button to your auth view (e.g., resources/views/auth/login.blade.php):
<a href="{{ route('google.login') }}" class="btn btn-google">
Sign in with Google
</a>
Ensure the route is registered in routes/web.php:
Route::get('/auth/google', [GoogleAuthController::class, 'redirectToGoogle'])->name('google.login');
Route::get('/auth/google/callback', [GoogleAuthController::class, 'handleGoogleCallback']);
User Model Integration
Extend your User model to include Google-specific fields (e.g., google_id, google_email):
use Birko\MiniShopGoogle\Traits\HasGoogleAccount;
class User extends Authenticatable
{
use HasGoogleAccount;
}
Redirect to Google
Use the GoogleAuthController to initiate OAuth flow:
public function redirectToGoogle()
{
return Socialite::driver('google')->redirect();
}
Handle Callback Process the Google response and create/update the user:
public function handleGoogleCallback()
{
try {
$googleUser = Socialite::driver('google')->user();
$user = User::updateOrCreate(
['google_id' => $googleUser->id],
[
'name' => $googleUser->name,
'email' => $googleUser->email,
'google_email_verified' => $googleUser->email_verified_at !== null,
]
);
auth()->login($user);
return redirect()->intended('/dashboard');
} catch (\Exception $e) {
return redirect('/login')->with('error', 'Google login failed: ' . $e->getMessage());
}
}
User Sync
Override the syncGoogleAccount method in your User model to customize sync logic:
public function syncGoogleAccount(array $googleData)
{
$this->update([
'name' => $googleData['name'],
'email' => $googleData['email'],
'avatar' => $googleData['avatar'] ?? null,
]);
}
Laravel Socialite
Ensure laravel/socialite is installed (composer require laravel/socialite).
Configure the Google provider in config/services.php:
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'),
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
'redirect' => env('GOOGLE_REDIRECT_URI'),
],
MiniShop Compatibility
Extend the package’s GoogleAuthService to integrate with MiniShop’s user system:
use Birko\MiniShopGoogle\Services\GoogleAuthService;
class CustomGoogleAuthService extends GoogleAuthService
{
public function findOrCreateUser(array $googleData)
{
return User::firstOrCreate(
['email' => $googleData['email']],
[
'name' => $googleData['name'],
'google_id' => $googleData['id'],
]
);
}
}
Middleware for Authenticated Users Protect routes requiring Google-authenticated users:
Route::middleware(['auth', 'google.auth'])->group(function () {
// Routes for Google-authenticated users
});
Missing .env Configuration
Ensure GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, and GOOGLE_REDIRECT_URI are set in .env.
Redirect URI must match exactly what’s registered in Google Cloud Console.
Scope Mismatches
If email or profile scopes are missing, the OAuth flow may fail silently. Verify scopes in config/minishop-google.php:
'scopes' => ['email', 'profile', 'openid'],
User Model Conflicts
The package assumes a google_id field in the users table. If your model uses a different field name, override the getGoogleIdAttribute method:
public function getGoogleIdAttribute()
{
return $this->google_user_id; // Custom field name
}
CSRF Token Issues
Google OAuth callbacks may fail if CSRF protection is enabled. Add this to your App\Http\Middleware\VerifyCsrfToken:
protected $except = [
'auth/google/callback',
];
Enable Socialite Debugging
Add this to config/services.php to log OAuth responses:
'debug' => env('APP_DEBUG', false),
Check Google API Console Verify the OAuth credentials and authorized redirect URIs in Google Cloud Console.
Log Google User Data
Dump the $googleUser object in handleGoogleCallback to inspect received data:
\Log::info('Google User Data:', $googleUser->toArray());
Custom User Provider
Override the UserProvider to integrate with MiniShop’s user system:
use Birko\MiniShopGoogle\Providers\UserProvider as GoogleUserProvider;
class CustomUserProvider extends GoogleUserProvider
{
public function retrieveByGoogleId($googleId)
{
return MiniShopUser::where('google_id', $googleId)->first();
}
}
Add Custom Claims
Extend the GoogleAuthService to fetch additional user data from Google:
public function getUserData()
{
$data = parent::getUserData();
$data['custom_claim'] = $this->getCustomClaimFromGoogle();
return $data;
}
Webhook Integration Use Google’s People API to sync user updates via webhooks:
// Example: Listen for Google account changes
Event::listen('google.account.updated', function ($user) {
// Sync MiniShop user data
});
How can I help you explore Laravel packages today?