Xboard/app/Services/AuthService.php
2025-01-07 01:20:11 +08:00

51 lines
1.2 KiB
PHP

<?php
namespace App\Services;
use App\Models\User;
use App\Utils\CacheKey;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Laravel\Sanctum\NewAccessToken;
use Illuminate\Support\Str;
class AuthService
{
private User $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function generateAuthData(): array
{
// Create a new Sanctum token with device info
$token = $this->user->createToken(
Str::random(20), // token name (device identifier)
['*'], // abilities
now()->addYear() // expiration
);
// Format token: remove ID prefix and add Bearer
$tokenParts = explode('|', $token->plainTextToken);
$formattedToken = 'Bearer ' . ($tokenParts[1] ?? $tokenParts[0]);
return [
'auth_data' => $formattedToken,
'is_admin' => $this->user->is_admin,
];
}
public function getSessions(): array
{
return $this->user->tokens()->get()->toArray();
}
public function removeSession(string $sessionId): bool
{
$this->user->tokens()->delete();
return true;
}
}