trypost/app/Http/Controllers/Auth/TikTokController.php

109 lines
3.6 KiB
PHP
Raw Normal View History

2026-01-15 01:13:44 +00:00
<?php
declare(strict_types=1);
2026-01-15 01:13:44 +00:00
namespace App\Http\Controllers\Auth;
use App\Enums\SocialAccount\Platform as SocialPlatform;
use App\Enums\SocialAccount\Status;
2026-01-15 01:13:44 +00:00
use App\Models\Workspace;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\View\View;
2026-01-15 01:13:44 +00:00
use Laravel\Socialite\Facades\Socialite;
use Symfony\Component\HttpFoundation\Response;
class TikTokController extends SocialController
{
protected string $driver = 'tiktok';
protected SocialPlatform $platform = SocialPlatform::TikTok;
protected array $scopes = [
'user.info.basic',
'user.info.profile',
'user.info.stats',
2026-01-15 01:13:44 +00:00
'video.publish',
'video.upload',
'video.list',
2026-01-15 01:13:44 +00:00
];
public function connect(Request $request): Response|RedirectResponse
2026-01-15 01:13:44 +00:00
{
$this->ensurePlatformEnabled();
$workspace = $request->user()->currentWorkspace;
if (! $workspace) {
return redirect()->route('app.workspaces.create');
}
2026-01-15 17:24:39 +00:00
$this->authorize('manageAccounts', $workspace);
2026-01-15 01:13:44 +00:00
session(['social_reconnect_id' => null]);
return $this->redirectToProvider($request, $this->driver, $this->scopes);
2026-01-15 01:13:44 +00:00
}
public function callback(Request $request): View
2026-01-15 01:13:44 +00:00
{
$workspaceId = session('social_connect_workspace');
if (! $workspaceId) {
return $this->popupCallback(false, __('accounts.popup_callback.session_expired'), $this->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)) {
return $this->popupCallback(false, __('accounts.popup_callback.workspace_not_found'), $this->platform->value);
2026-01-15 01:13:44 +00:00
}
try {
$socialUser = Socialite::driver($this->driver)
->scopes($this->scopes)
->user();
Log::info('TikTok OAuth User Data', [
'nickname' => $socialUser->getNickname(),
'user' => $socialUser->user ?? [],
'attributes' => $socialUser->attributes ?? [],
]);
// TikTok returns username via getNickname() when user.info.profile scope is included
$username = $socialUser->getNickname();
$avatarPath = uploadFromUrl($socialUser->getAvatar());
$workspace->socialAccounts()->updateOrCreate(
[
'platform' => $this->platform->value,
'platform_user_id' => $socialUser->getId(),
],
[
'username' => $username,
'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,
'status' => Status::Connected,
'error_message' => null,
'disconnected_at' => null,
],
);
2026-01-15 01:13:44 +00:00
session()->forget('social_reconnect_id');
2026-01-15 01:13:44 +00:00
return $this->popupCallback(true, __('accounts.popup_callback.connected'), $this->platform->value);
2026-01-15 01:13:44 +00:00
} catch (\Exception $e) {
Log::error('TikTok OAuth Error', [
'error' => $e->getMessage(),
]);
return $this->popupCallback(false, __('accounts.popup_callback.error_connecting'), $this->platform->value);
2026-01-15 01:13:44 +00:00
}
}
}