2026-01-15 01:13:44 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
|
2026-01-17 17:44:37 +00:00
|
|
|
use App\Enums\SocialAccount\Platform as SocialPlatform;
|
|
|
|
|
use App\Enums\SocialAccount\Status;
|
2026-01-15 01:13:44 +00:00
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
use App\Models\SocialAccount;
|
|
|
|
|
use App\Models\Workspace;
|
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Log;
|
2026-01-17 02:46:30 +00:00
|
|
|
use Illuminate\View\View;
|
2026-01-15 01:13:44 +00:00
|
|
|
use Inertia\Inertia;
|
|
|
|
|
use Inertia\Response;
|
|
|
|
|
use Laravel\Socialite\Facades\Socialite;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
|
|
|
|
|
|
|
|
|
class SocialController extends Controller
|
|
|
|
|
{
|
2026-01-16 15:36:52 +00:00
|
|
|
protected SocialPlatform $platform;
|
|
|
|
|
|
|
|
|
|
protected function ensurePlatformEnabled(): void
|
|
|
|
|
{
|
|
|
|
|
if (isset($this->platform) && ! $this->platform->isEnabled()) {
|
|
|
|
|
abort(403, 'This platform is currently unavailable.');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
public function index(Request $request): Response|RedirectResponse
|
2026-01-15 01:13:44 +00:00
|
|
|
{
|
2026-01-17 02:46:30 +00:00
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
|
|
|
|
|
|
if (! $workspace) {
|
|
|
|
|
return redirect()->route('workspaces.create');
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 01:13:44 +00:00
|
|
|
$this->authorize('view', $workspace);
|
|
|
|
|
|
|
|
|
|
$connectedAccounts = $workspace->socialAccounts;
|
|
|
|
|
|
2026-01-16 15:36:52 +00:00
|
|
|
$platforms = collect(SocialPlatform::enabled())->map(function ($platform) use ($connectedAccounts) {
|
2026-01-15 01:13:44 +00:00
|
|
|
$connected = $connectedAccounts->firstWhere('platform', $platform);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
'value' => $platform->value,
|
|
|
|
|
'label' => $platform->label(),
|
|
|
|
|
'color' => $platform->color(),
|
|
|
|
|
'connected' => $connected !== null,
|
|
|
|
|
'account' => $connected,
|
|
|
|
|
];
|
2026-01-16 15:36:52 +00:00
|
|
|
})->values();
|
2026-01-15 01:13:44 +00:00
|
|
|
|
|
|
|
|
return Inertia::render('accounts/Index', [
|
|
|
|
|
'workspace' => $workspace,
|
|
|
|
|
'platforms' => $platforms,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
public function disconnect(Request $request, SocialAccount $account): RedirectResponse
|
2026-01-15 01:13:44 +00:00
|
|
|
{
|
2026-01-17 02:46:30 +00:00
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
|
|
|
|
|
|
if (! $workspace) {
|
|
|
|
|
return redirect()->route('workspaces.create');
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 17:24:39 +00:00
|
|
|
$this->authorize('manageAccounts', $workspace);
|
2026-01-15 01:13:44 +00:00
|
|
|
|
|
|
|
|
if ($account->workspace_id !== $workspace->id) {
|
|
|
|
|
abort(403);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$account->delete();
|
|
|
|
|
|
2026-01-15 17:24:39 +00:00
|
|
|
session()->flash('flash.banner', 'Account disconnected successfully!');
|
|
|
|
|
session()->flash('flash.bannerStyle', 'success');
|
|
|
|
|
|
|
|
|
|
return back();
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
protected function redirectToProvider(Request $request, string $driver, array $scopes): SymfonyResponse
|
2026-01-15 01:13:44 +00:00
|
|
|
{
|
2026-01-17 02:46:30 +00:00
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
|
|
|
|
|
|
if (! $workspace) {
|
|
|
|
|
return redirect()->route('workspaces.create');
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 01:13:44 +00:00
|
|
|
session(['social_connect_workspace' => $workspace->id]);
|
2026-01-17 02:46:30 +00:00
|
|
|
session(['social_connect_onboarding' => $request->boolean('onboarding')]);
|
2026-01-15 01:13:44 +00:00
|
|
|
|
|
|
|
|
return Inertia::location(
|
|
|
|
|
Socialite::driver($driver)
|
|
|
|
|
->scopes($scopes)
|
|
|
|
|
->redirect()
|
|
|
|
|
->getTargetUrl()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function handleCallback(
|
|
|
|
|
Request $request,
|
|
|
|
|
SocialPlatform $platform,
|
|
|
|
|
string $driver
|
2026-01-17 02:46:30 +00:00
|
|
|
): View {
|
2026-01-15 01:13:44 +00:00
|
|
|
$workspaceId = session('social_connect_workspace');
|
|
|
|
|
|
|
|
|
|
if (! $workspaceId) {
|
2026-01-17 02:46:30 +00:00
|
|
|
return $this->popupCallback(false, 'Session expired. Please try again.', $platform->value);
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$workspace = Workspace::find($workspaceId);
|
|
|
|
|
|
2026-01-15 17:24:39 +00:00
|
|
|
if (! $workspace || ! $request->user()->can('manageAccounts', $workspace)) {
|
2026-01-17 02:46:30 +00:00
|
|
|
return $this->popupCallback(false, 'Workspace not found.', $platform->value);
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
$socialUser = Socialite::driver($driver)->user();
|
2026-01-17 02:46:30 +00:00
|
|
|
$existingAccount = $workspace->socialAccounts()
|
|
|
|
|
->where('platform', $platform->value)
|
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
// If account exists and is connected, don't allow duplicate
|
|
|
|
|
if ($existingAccount && ! $existingAccount->isDisconnected()) {
|
|
|
|
|
return $this->popupCallback(false, 'This platform is already connected.', $platform->value);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-15 01:13:44 +00:00
|
|
|
$avatarPath = uploadFromUrl($socialUser->getAvatar());
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
if ($existingAccount) {
|
|
|
|
|
// Reconnect existing account
|
|
|
|
|
$existingAccount->update([
|
|
|
|
|
'platform_user_id' => $socialUser->getId(),
|
|
|
|
|
'username' => $socialUser->getNickname(),
|
|
|
|
|
'display_name' => $socialUser->getName(),
|
|
|
|
|
'avatar_url' => $avatarPath,
|
|
|
|
|
'access_token' => $socialUser->token,
|
|
|
|
|
'refresh_token' => $socialUser->refreshToken,
|
|
|
|
|
'token_expires_at' => $socialUser->expiresIn ? now()->addSeconds($socialUser->expiresIn) : null,
|
|
|
|
|
'scopes' => $socialUser->approvedScopes ?? null,
|
|
|
|
|
]);
|
|
|
|
|
$existingAccount->markAsConnected();
|
|
|
|
|
|
|
|
|
|
return $this->popupCallback(true, 'Account reconnected!', $platform->value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create new account
|
2026-01-15 01:13:44 +00:00
|
|
|
$workspace->socialAccounts()->create([
|
|
|
|
|
'platform' => $platform->value,
|
|
|
|
|
'platform_user_id' => $socialUser->getId(),
|
|
|
|
|
'username' => $socialUser->getNickname(),
|
|
|
|
|
'display_name' => $socialUser->getName(),
|
|
|
|
|
'avatar_url' => $avatarPath,
|
|
|
|
|
'access_token' => $socialUser->token,
|
|
|
|
|
'refresh_token' => $socialUser->refreshToken,
|
|
|
|
|
'token_expires_at' => $socialUser->expiresIn ? now()->addSeconds($socialUser->expiresIn) : null,
|
|
|
|
|
'scopes' => $socialUser->approvedScopes ?? null,
|
2026-01-17 02:46:30 +00:00
|
|
|
'status' => Status::Connected,
|
2026-01-15 01:13:44 +00:00
|
|
|
]);
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
return $this->popupCallback(true, 'Account connected!', $platform->value);
|
2026-01-15 01:13:44 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
Log::error('Social OAuth Error', [
|
|
|
|
|
'platform' => $platform->value,
|
|
|
|
|
'error' => $e->getMessage(),
|
|
|
|
|
]);
|
|
|
|
|
|
2026-01-17 02:46:30 +00:00
|
|
|
return $this->popupCallback(false, 'Error connecting account. Please try again.', $platform->value);
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
2026-01-17 02:46:30 +00:00
|
|
|
|
|
|
|
|
protected function forgetSocialConnectSession(): void
|
|
|
|
|
{
|
|
|
|
|
session()->forget(['social_connect_workspace', 'social_connect_onboarding']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getRedirectRoute(): string
|
|
|
|
|
{
|
|
|
|
|
return session('social_connect_onboarding', false) ? 'onboarding.step2' : 'accounts';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Return a view that closes the popup and notifies the parent window.
|
|
|
|
|
*/
|
|
|
|
|
protected function popupCallback(bool $success, string $message, ?string $platform = null): View
|
|
|
|
|
{
|
|
|
|
|
$this->forgetSocialConnectSession();
|
|
|
|
|
|
|
|
|
|
return view('auth.social-callback', [
|
|
|
|
|
'success' => $success,
|
|
|
|
|
'message' => $message,
|
|
|
|
|
'platform' => $platform,
|
|
|
|
|
]);
|
|
|
|
|
}
|
2026-01-15 01:13:44 +00:00
|
|
|
}
|