- New card-based layout showing connected accounts with avatar, platform badge, brand label, and connection date - "Add Social" button opens dialog with platform grid for connecting - Removed unique constraint on workspace_id+platform to allow multiple accounts per platform - Removed "already connected" checks from all 13 OAuth controllers - Updated tests to verify multi-account connection works
304 lines
12 KiB
PHP
304 lines
12 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Enums\SocialAccount\Platform as SocialPlatform;
|
|
use App\Enums\SocialAccount\Status;
|
|
use App\Models\Workspace;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\View\View;
|
|
use Inertia\Inertia;
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
use Symfony\Component\HttpFoundation\Response;
|
|
|
|
class YouTubeController extends SocialController
|
|
{
|
|
protected string $driver = 'google';
|
|
|
|
protected SocialPlatform $platform = SocialPlatform::YouTube;
|
|
|
|
protected array $scopes = [
|
|
'https://www.googleapis.com/auth/youtube.upload',
|
|
'https://www.googleapis.com/auth/youtube.readonly',
|
|
'https://www.googleapis.com/auth/youtube.force-ssl',
|
|
'https://www.googleapis.com/auth/yt-analytics.readonly',
|
|
];
|
|
|
|
public function connect(Request $request): Response|RedirectResponse
|
|
{
|
|
$this->ensurePlatformEnabled();
|
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
if (! $workspace) {
|
|
return redirect()->route('app.workspaces.create');
|
|
}
|
|
|
|
$this->authorize('manageAccounts', $workspace);
|
|
$this->ensureSocialAccountLimit($workspace);
|
|
|
|
session([
|
|
'social_connect_workspace' => $workspace->id,
|
|
'social_reconnect_id' => null,
|
|
'social_connect_onboarding' => $request->boolean('onboarding'),
|
|
]);
|
|
|
|
return $this->redirectToGoogle();
|
|
}
|
|
|
|
public function callback(Request $request): View|RedirectResponse
|
|
{
|
|
$workspaceId = session('social_connect_workspace');
|
|
|
|
if (! $workspaceId) {
|
|
return $this->popupCallback(false, 'Session expired. Please try again.', $this->platform->value);
|
|
}
|
|
|
|
$workspace = Workspace::find($workspaceId);
|
|
|
|
if (! $workspace || ! $request->user()->can('manageAccounts', $workspace)) {
|
|
return $this->popupCallback(false, 'Workspace not found.', $this->platform->value);
|
|
}
|
|
|
|
try {
|
|
$socialUser = Socialite::driver($this->driver)->user();
|
|
|
|
// Fetch the channels the user authorized
|
|
$channels = $this->fetchChannels($socialUser->token);
|
|
|
|
if (empty($channels)) {
|
|
return $this->popupCallback(false, 'No YouTube channels found. Please create a channel first.', $this->platform->value);
|
|
}
|
|
|
|
// If only one channel, connect directly (most common case)
|
|
if (count($channels) === 1) {
|
|
$channel = $channels[0];
|
|
$avatarPath = uploadFromUrl(data_get($channel, 'thumbnail'));
|
|
|
|
// Create new account
|
|
$workspace->socialAccounts()->create([
|
|
'platform' => $this->platform->value,
|
|
'platform_user_id' => data_get($channel, 'id'),
|
|
'username' => ltrim(data_get($channel, 'custom_url', data_get($channel, 'id')), '@'),
|
|
'display_name' => data_get($channel, 'title'),
|
|
'avatar_url' => $avatarPath,
|
|
'access_token' => $socialUser->token,
|
|
'refresh_token' => $socialUser->refreshToken,
|
|
'token_expires_at' => $socialUser->expiresIn ? now()->addSeconds($socialUser->expiresIn) : null,
|
|
'scopes' => $this->scopes,
|
|
'status' => Status::Connected,
|
|
'meta' => [
|
|
'channel_id' => data_get($channel, 'id'),
|
|
'google_user_id' => $socialUser->getId(),
|
|
],
|
|
]);
|
|
|
|
return $this->popupCallback(true, 'YouTube channel connected!', $this->platform->value);
|
|
}
|
|
|
|
// Multiple channels - store data and show selection screen
|
|
session([
|
|
'youtube_oauth' => [
|
|
'access_token' => $socialUser->token,
|
|
'refresh_token' => $socialUser->refreshToken,
|
|
'expires_in' => $socialUser->expiresIn,
|
|
'user_id' => $socialUser->getId(),
|
|
],
|
|
]);
|
|
|
|
return redirect()->route('app.social.youtube.select-channel');
|
|
} catch (\Exception $e) {
|
|
Log::error('YouTube OAuth Error', [
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
return $this->popupCallback(false, 'Error connecting account. Please try again.', $this->platform->value);
|
|
}
|
|
}
|
|
|
|
public function selectChannel(Request $request)
|
|
{
|
|
$oauthData = session('youtube_oauth');
|
|
$workspaceId = session('social_connect_workspace');
|
|
|
|
if (! $oauthData || ! $workspaceId) {
|
|
session()->flash('flash.banner', __('accounts.flash.session_expired'));
|
|
session()->flash('flash.bannerStyle', 'danger');
|
|
|
|
return redirect()->route('app.accounts');
|
|
}
|
|
|
|
$workspace = Workspace::find($workspaceId);
|
|
|
|
if (! $workspace) {
|
|
session()->flash('flash.banner', __('accounts.flash.workspace_not_found'));
|
|
session()->flash('flash.bannerStyle', 'danger');
|
|
|
|
return redirect()->route('app.accounts');
|
|
}
|
|
|
|
// Fetch YouTube channels
|
|
$channels = $this->fetchChannels(data_get($oauthData, 'access_token'));
|
|
|
|
if (empty($channels)) {
|
|
$redirectRoute = $this->getRedirectRoute();
|
|
$this->forgetSocialConnectSession();
|
|
session()->forget('youtube_oauth');
|
|
|
|
session()->flash('flash.banner', __('accounts.flash.no_youtube_channels'));
|
|
session()->flash('flash.bannerStyle', 'danger');
|
|
|
|
return redirect()->route($redirectRoute);
|
|
}
|
|
|
|
return inertia('accounts/YouTubeChannelSelect', [
|
|
'workspace' => $workspace,
|
|
'channels' => $channels,
|
|
]);
|
|
}
|
|
|
|
public function select(Request $request): View
|
|
{
|
|
$request->validate([
|
|
'channel_id' => 'required|string',
|
|
]);
|
|
|
|
$oauthData = session('youtube_oauth');
|
|
$workspaceId = session('social_connect_workspace');
|
|
|
|
if (! $oauthData || ! $workspaceId) {
|
|
return $this->popupCallback(false, 'Session expired. Please try again.', $this->platform->value);
|
|
}
|
|
|
|
$workspace = Workspace::find($workspaceId);
|
|
|
|
if (! $workspace || ! $request->user()->can('manageAccounts', $workspace)) {
|
|
return $this->popupCallback(false, 'Workspace not found.', $this->platform->value);
|
|
}
|
|
|
|
try {
|
|
$channels = $this->fetchChannels(data_get($oauthData, 'access_token'));
|
|
$selectedChannel = collect($channels)->firstWhere('id', $request->channel_id);
|
|
|
|
if (! $selectedChannel) {
|
|
return $this->popupCallback(false, 'Channel not found.', $this->platform->value);
|
|
}
|
|
|
|
$avatarPath = uploadFromUrl(data_get($selectedChannel, 'thumbnail'));
|
|
$reconnectId = data_get($oauthData, 'reconnect_id', null);
|
|
|
|
if ($reconnectId) {
|
|
// Reconnect existing account
|
|
$existingAccount = $workspace->socialAccounts()->find($reconnectId);
|
|
|
|
if ($existingAccount) {
|
|
$existingAccount->update([
|
|
'platform_user_id' => data_get($selectedChannel, 'id'),
|
|
'username' => ltrim(data_get($selectedChannel, 'custom_url', data_get($selectedChannel, 'id')), '@'),
|
|
'display_name' => data_get($selectedChannel, 'title'),
|
|
'avatar_url' => $avatarPath,
|
|
'access_token' => data_get($oauthData, 'access_token'),
|
|
'refresh_token' => data_get($oauthData, 'refresh_token'),
|
|
'token_expires_at' => data_get($oauthData, 'expires_in') ? now()->addSeconds(data_get($oauthData, 'expires_in')) : null,
|
|
'scopes' => $this->scopes,
|
|
'meta' => [
|
|
'channel_id' => data_get($selectedChannel, 'id'),
|
|
'google_user_id' => data_get($oauthData, 'user_id'),
|
|
],
|
|
]);
|
|
$existingAccount->markAsConnected();
|
|
|
|
session()->forget(['youtube_oauth', 'social_reconnect_id']);
|
|
|
|
return $this->popupCallback(true, 'YouTube channel reconnected!', $this->platform->value);
|
|
}
|
|
}
|
|
|
|
// Create new account
|
|
$workspace->socialAccounts()->create([
|
|
'platform' => $this->platform->value,
|
|
'platform_user_id' => data_get($selectedChannel, 'id'),
|
|
'username' => ltrim(data_get($selectedChannel, 'custom_url', data_get($selectedChannel, 'id')), '@'),
|
|
'display_name' => data_get($selectedChannel, 'title'),
|
|
'avatar_url' => $avatarPath,
|
|
'access_token' => data_get($oauthData, 'access_token'),
|
|
'refresh_token' => data_get($oauthData, 'refresh_token'),
|
|
'token_expires_at' => data_get($oauthData, 'expires_in') ? now()->addSeconds(data_get($oauthData, 'expires_in')) : null,
|
|
'scopes' => $this->scopes,
|
|
'status' => Status::Connected,
|
|
'meta' => [
|
|
'channel_id' => data_get($selectedChannel, 'id'),
|
|
'google_user_id' => data_get($oauthData, 'user_id'),
|
|
],
|
|
]);
|
|
|
|
session()->forget(['youtube_oauth', 'social_reconnect_id']);
|
|
|
|
return $this->popupCallback(true, 'YouTube channel connected!', $this->platform->value);
|
|
} catch (\Exception $e) {
|
|
Log::error('YouTube channel selection error', [
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
|
|
return $this->popupCallback(false, 'Error connecting channel. Please try again.', $this->platform->value);
|
|
}
|
|
}
|
|
|
|
private function redirectToGoogle(): Response
|
|
{
|
|
return Inertia::location(
|
|
Socialite::driver($this->driver)
|
|
->scopes($this->scopes)
|
|
->with([
|
|
'access_type' => 'offline',
|
|
'prompt' => 'consent',
|
|
'include_granted_scopes' => 'true',
|
|
])
|
|
->redirect()
|
|
->getTargetUrl()
|
|
);
|
|
}
|
|
|
|
private function fetchChannels(string $accessToken): array
|
|
{
|
|
try {
|
|
$response = Http::withToken($accessToken)
|
|
->get('https://www.googleapis.com/youtube/v3/channels', [
|
|
'part' => 'snippet,contentDetails,statistics',
|
|
'mine' => 'true',
|
|
]);
|
|
|
|
if ($response->failed()) {
|
|
Log::error('YouTube channels fetch failed', [
|
|
'status' => $response->status(),
|
|
'body' => $response->body(),
|
|
]);
|
|
|
|
return [];
|
|
}
|
|
|
|
$data = $response->json();
|
|
|
|
return collect(data_get($data, 'items', []))->map(fn ($channel) => [
|
|
'id' => data_get($channel, 'id'),
|
|
'title' => data_get($channel, 'snippet.title'),
|
|
'description' => data_get($channel, 'snippet.description', ''),
|
|
'thumbnail' => data_get($channel, 'snippet.thumbnails.default.url'),
|
|
'custom_url' => data_get($channel, 'snippet.customUrl'),
|
|
'subscriber_count' => data_get($channel, 'statistics.subscriberCount', 0),
|
|
])->toArray();
|
|
} catch (\Exception $e) {
|
|
Log::error('YouTube channels fetch error', [
|
|
'error' => $e->getMessage(),
|
|
]);
|
|
|
|
return [];
|
|
}
|
|
}
|
|
}
|