Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dmag's avatar
Level 6

How to connect users' calendars to my app?

In the app users can schedule some events, and I need to have that event to created in the user's google calendar that the user had previously connected to their saas app account. I've explored laravel google calendar package, but it requires manual setup and seems to work only for a single user.

How can I make it so that many users, can authorize the app to add events to their google calendar?

0 likes
2 replies
LaryAI's avatar
Level 58

To connect multiple users' Google Calendars to your SaaS app and allow them to schedule events, you can use OAuth 2.0 for user authentication and authorization. Here's a step-by-step guide to achieve this:

  1. Set Up Google API Credentials:

    • Go to the Google Cloud Console.
    • Create a new project or select an existing one.
    • Enable the Google Calendar API for your project.
    • Create OAuth 2.0 credentials (Client ID and Client Secret) for a web application.
  2. Install Required Packages:

    • Use the spatie/laravel-google-calendar package for interacting with Google Calendar.
    • Use the socialiteproviders/google package for OAuth authentication.

    Install the packages via Composer:

    composer require spatie/laravel-google-calendar socialiteproviders/google
    
  3. Configure Socialite:

    • Add the Google provider to your config/services.php:
    'google' => [
        'client_id' => env('GOOGLE_CLIENT_ID'),
        'client_secret' => env('GOOGLE_CLIENT_SECRET'),
        'redirect' => env('GOOGLE_REDIRECT_URI'),
    ],
    
    • Add the environment variables to your .env file:
    GOOGLE_CLIENT_ID=your-client-id
    GOOGLE_CLIENT_SECRET=your-client-secret
    GOOGLE_REDIRECT_URI=https://your-app.com/auth/google/callback
    
  4. Set Up Routes and Controllers:

    • Define routes for Google OAuth:
    // routes/web.php
    Route::get('auth/google', [GoogleController::class, 'redirectToGoogle']);
    Route::get('auth/google/callback', [GoogleController::class, 'handleGoogleCallback']);
    
    • Create a controller to handle the OAuth flow:
    // app/Http/Controllers/GoogleController.php
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use Socialite;
    use App\Models\User;
    
    class GoogleController extends Controller
    {
        public function redirectToGoogle()
        {
            return Socialite::driver('google')
                ->scopes(['https://www.googleapis.com/auth/calendar'])
                ->redirect();
        }
    
        public function handleGoogleCallback()
        {
            $user = Socialite::driver('google')->stateless()->user();
    
            // Save the user's token and refresh token
            $authUser = User::updateOrCreate(
                ['email' => $user->getEmail()],
                [
                    'name' => $user->getName(),
                    'google_id' => $user->getId(),
                    'google_token' => $user->token,
                    'google_refresh_token' => $user->refreshToken,
                ]
            );
    
            auth()->login($authUser, true);
    
            return redirect()->route('home');
        }
    }
    
  5. Store and Use Tokens:

    • Ensure your users table has columns for google_id, google_token, and google_refresh_token.
    • Use the stored tokens to interact with the Google Calendar API.
  6. Create Events in Google Calendar:

    • Use the spatie/laravel-google-calendar package to create events:
    // In your controller or service
    use Spatie\GoogleCalendar\Event;
    use Carbon\Carbon;
    
    public function createGoogleCalendarEvent($user, $eventDetails)
    {
        $googleClient = new \Google_Client();
        $googleClient->setAccessToken($user->google_token);
    
        if ($googleClient->isAccessTokenExpired()) {
            $googleClient->fetchAccessTokenWithRefreshToken($user->google_refresh_token);
            $user->google_token = $googleClient->getAccessToken();
            $user->save();
        }
    
        $event = new Event;
        $event->name = $eventDetails['name'];
        $event->startDateTime = Carbon::parse($eventDetails['start']);
        $event->endDateTime = Carbon::parse($eventDetails['end']);
        $event->save();
    }
    

By following these steps, you can allow multiple users to connect their Google Calendars to your SaaS app and schedule events directly in their calendars.

martinbean's avatar

I've explored laravel google calendar package, but it requires manual setup and seems to work only for a single user.

How can I make it so that many users, can authorize the app to add events to their google calendar?

@dmag If you’re using the Google API then you will get an access token for the current user when they authorise your app’s access to their Google calendar. You need to store the access token for each user. With this access token, you can then manage events for that user.

Please or to participate in this conversation.