Skip to content

Instantly share code, notes, and snippets.

@osbre
Last active February 1, 2026 09:49
Show Gist options
  • Select an option

  • Save osbre/e0c95742341678ddf345b34399c572a8 to your computer and use it in GitHub Desktop.

Select an option

Save osbre/e0c95742341678ddf345b34399c572a8 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace App\Http\Controllers\Dashboard;
use App\Models\Connection;
use App\Models\ConnectionProvider;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Inertia\Response;
use Spatie\RouteAttributes\Attributes\Get;
class ConnectionController
{
#[Get('connections', name: 'connections.index')]
public function index(Request $request): Response
{
return inertia('Connections/Index');
}
#[Get('connections/{provider}/redirect', name: 'connections.redirect')]
public function redirect(ConnectionProvider $provider): RedirectResponse
{
return $provider->driver()->redirect();
}
#[Get('connections/{provider}/callback', name: 'connections.callback')]
public function callback(ConnectionProvider $provider, Request $request): RedirectResponse
{
$data = $provider->driver()->user();
$owner = $provider->ownedByUser() ? $user : $user->currentTeam;
$connection = new Connection([
'provider' => $provider->value,
'provider_id' => $data->getId(),
'access_token' => $data->token,
'refresh_token' => $data->refreshToken,
'token_expires_at' => $data->expiresIn ? now()->addSeconds($data->expiresIn) : null,
]);
$owner->connections()->save($connection);
return redirect()->route('connections.index')->with('status', "{$provider->label()} connected successfully!");
}
}
<?php
declare(strict_types=1);
namespace App\Models;
use Laravel\Socialite\Socialite;
use Laravel\Socialite\Two\GoogleProvider;
use SocialiteProviders\Notion\Provider as NotionProvider;
enum ConnectionProvider: string
{
case Google = 'google';
case Notion = 'notion';
public function label(): string
{
return match ($this) {
self::Google => 'Google',
self::Notion => 'Notion',
};
}
public function ownedByUser(): bool
{
return match ($this) {
self::Google => true,
self::Notion => false,
};
}
public function driver(): GoogleProvider|NotionProvider
{
return Socialite::driver($this->value)->scopes($this->driverScopes());
}
/** @return array<int, string> */
private function driverScopes(): array
{
return match ($this) {
self::Google => ['openid', 'email', 'profile', 'https://www.googleapis.com/auth/drive.file'],
self::Notion => [],
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment