ensurePlatformEnabled(); $workspace = $request->user()->currentWorkspace; if (! $workspace) { return redirect()->route('app.workspaces.create'); } $this->authorize('manageAccounts', $workspace); return $this->redirectToProvider($request, $this->driver, $this->scopes); } public function callback(Request $request): View { $workspaceId = session('social_connect_workspace'); if (! $workspaceId) { return $this->popupCallback(false, __('accounts.popup_callback.session_expired'), $this->platform->value); } $workspace = Workspace::find($workspaceId); if (! $workspace || ! $request->user()->can('manageAccounts', $workspace)) { return $this->popupCallback(false, __('accounts.popup_callback.workspace_not_found'), $this->platform->value); } try { $socialUser = Socialite::driver($this->driver)->user(); $avatarPath = uploadFromUrl($socialUser->getAvatar()); // Create new account $workspace->socialAccounts()->create([ 'platform' => $this->platform->value, 'platform_user_id' => $socialUser->getId(), 'username' => $socialUser->getNickname(), 'display_name' => $socialUser->getName() ?? $socialUser->getNickname(), 'avatar_url' => $avatarPath, 'access_token' => $socialUser->token, 'refresh_token' => $socialUser->refreshToken, 'token_expires_at' => $socialUser->expiresIn ? now()->addSeconds($socialUser->expiresIn) : now()->addDays(30), // Pinterest returns scopes space-joined but Socialite doesn't split them, so re-split here. 'scopes' => explode(' ', implode(' ', $socialUser->approvedScopes)), 'status' => Status::Connected, ]); return $this->popupCallback(true, __('accounts.popup_callback.connected'), $this->platform->value); } catch (\Exception $e) { Log::error('Pinterest OAuth Error', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); return $this->popupCallback(false, __('accounts.popup_callback.error_connecting'), $this->platform->value); } } }