39 lines
No EOL
1 KiB
PHP
39 lines
No EOL
1 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Auth;
|
|
|
|
use App\Enums\PlatformEnum;
|
|
use App\Exceptions\PlatformAuthException;
|
|
use App\Modules\Lemmy\Services\LemmyApiService;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class LemmyAuthService
|
|
{
|
|
public static function getToken(): string
|
|
{
|
|
$cachedToken = Cache::get('lemmy_jwt_token');
|
|
|
|
if ($cachedToken) {
|
|
return $cachedToken;
|
|
}
|
|
|
|
$username = config('lemmy.username');
|
|
$password = config('lemmy.password');
|
|
$instance = config('lemmy.instance');
|
|
|
|
if (!$username || !$password || !$instance) {
|
|
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Missing credentials or instance');
|
|
}
|
|
|
|
$api = new LemmyApiService($instance);
|
|
$token = $api->login($username, $password);
|
|
|
|
if (!$token) {
|
|
throw new PlatformAuthException(PlatformEnum::LEMMY, 'Login failed');
|
|
}
|
|
|
|
Cache::put('lemmy_jwt_token', $token, 3600);
|
|
|
|
return $token;
|
|
}
|
|
} |