trypost/app/Http/Controllers/Auth/BlueskyController.php
Paulo Castellano cd2798fd8d refactor(social): use native Inertia for all connect popup flows
Replace the per-platform native form POST + manual CSRF + JSON/Blade
popup callback with a single Inertia mechanism:

- popupCallback() renders the accounts/PopupCallback Inertia page (notifies
  the opener + closes the popup) for both the GET OAuth callbacks and the
  selection submits. Drops the auth.social-callback Blade view, the
  expectsJson JSON branch, and useHttp/useSocialConnect on the frontend.
- Selection/credential pages (LinkedIn, Facebook, Instagram, Bluesky,
  Mastodon) use Inertia useForm: automatic CSRF + native validation errors.
- Unify the three selection screens on one row + View/Choose layout; the
  LinkedIn company tag now uses a building icon.
- Bluesky auth failures throw ValidationException (422 for XHR, redirect
  back with errors otherwise).

Controller tests updated from assertViewIs/assertViewHas to assertInertia.
2026-06-25 13:54:16 -03:00

127 lines
4.4 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\Services\Social\BlueskyLexicon;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
use Inertia\Inertia;
use Inertia\Response;
class BlueskyController extends SocialController
{
protected SocialPlatform $platform = SocialPlatform::Bluesky;
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);
return Inertia::render('accounts/BlueskyConnect', [
'errors' => session('errors')?->getBag('default')?->toArray() ?? [],
]);
}
public function store(Request $request): Response|RedirectResponse
{
$this->ensurePlatformEnabled();
$request->validate([
'identifier' => 'required|string',
'password' => 'required|string|min:3',
]);
$workspace = $request->user()->currentWorkspace;
if (! $workspace) {
return redirect()->route('app.workspaces.create');
}
$this->authorize('manageAccounts', $workspace);
$service = config('trypost.platforms.bluesky.default_service');
try {
// Authenticate with Bluesky
$response = Http::post("{$service}/xrpc/".BlueskyLexicon::CREATE_SESSION, [
'identifier' => $request->identifier,
'password' => $request->password,
]);
if ($response->failed()) {
Log::error('Bluesky authentication failed', [
'status' => $response->status(),
'body' => $response->body(),
]);
$errorMessage = 'Invalid credentials';
$body = $response->json();
if (isset($body['message'])) {
$errorMessage = $body['message'];
}
throw ValidationException::withMessages(['password' => $errorMessage]);
}
$data = $response->json();
// Get profile
$profileResponse = Http::withToken(data_get($data, 'accessJwt'))
->get("{$service}/xrpc/".BlueskyLexicon::GET_PROFILE, [
'actor' => data_get($data, 'did'),
]);
$profile = $profileResponse->successful() ? $profileResponse->json() : [];
$avatarPath = data_get($profile, 'avatar') ? uploadFromUrl(data_get($profile, 'avatar')) : null;
$workspace->socialAccounts()->updateOrCreate(
[
'platform' => $this->platform->value,
'platform_user_id' => data_get($data, 'did'),
],
[
'username' => data_get($data, 'handle'),
'display_name' => data_get($profile, 'displayName', data_get($data, 'handle')),
'avatar_url' => $avatarPath,
'access_token' => data_get($data, 'accessJwt'),
'refresh_token' => data_get($data, 'refreshJwt'),
'token_expires_at' => now()->addHours(2),
'status' => Status::Connected,
'error_message' => null,
'disconnected_at' => null,
'meta' => [
'service' => $service,
'identifier' => $request->identifier,
'password' => encrypt($request->password),
],
],
);
return $this->popupCallback(true, __('accounts.popup_callback.connected'), $this->platform->value);
} catch (ValidationException $e) {
throw $e;
} catch (\Exception $e) {
Log::error('Bluesky connection error', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
throw ValidationException::withMessages(['password' => 'Error connecting to Bluesky. Please try again.']);
}
}
}