Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Minishop Google Laravel Package

birko/minishop-google

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require birko/minishop-google
    

    Publish the package configuration (if needed):

    php artisan vendor:publish --provider="Birko\MiniShopGoogle\MiniShopGoogleServiceProvider"
    
  2. Configuration Edit config/minishop-google.php to include:

    • Google API credentials (Client ID, Client Secret, Redirect URI)
    • Scopes (e.g., profile, email, openid)
    • Whitelisted domains (if applicable)
  3. 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']);
    
  4. 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;
    }
    

Implementation Patterns

Workflow: OAuth Integration

  1. Redirect to Google Use the GoogleAuthController to initiate OAuth flow:

    public function redirectToGoogle()
    {
        return Socialite::driver('google')->redirect();
    }
    
  2. 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());
        }
    }
    
  3. 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,
        ]);
    }
    

Integration Tips

  • 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
    });
    

Gotchas and Tips

Pitfalls

  1. 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.

  2. 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'],
    
  3. 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
    }
    
  4. 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',
    ];
    

Debugging

  1. Enable Socialite Debugging Add this to config/services.php to log OAuth responses:

    'debug' => env('APP_DEBUG', false),
    
  2. Check Google API Console Verify the OAuth credentials and authorized redirect URIs in Google Cloud Console.

  3. Log Google User Data Dump the $googleUser object in handleGoogleCallback to inspect received data:

    \Log::info('Google User Data:', $googleUser->toArray());
    

Extension Points

  1. 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();
        }
    }
    
  2. 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;
    }
    
  3. 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
    });
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky