koel/app/Services/TokenManager.php

38 lines
898 B
PHP
Raw Normal View History

2020-09-06 18:21:39 +00:00
<?php
namespace App\Services;
use App\Models\User;
use Laravel\Sanctum\NewAccessToken;
2020-09-06 21:11:48 +00:00
use Laravel\Sanctum\PersonalAccessToken;
2020-09-06 18:21:39 +00:00
class TokenManager
{
public function createToken(User $user, array $abilities = ['*']): NewAccessToken
{
return $user->createToken(config('app.name'), $abilities);
}
public function destroyTokens(User $user): void
{
$user->tokens()->delete();
}
2020-09-06 21:11:48 +00:00
public function deleteTokenByPlainTextToken(string $plainTextToken): void
{
2022-07-27 15:32:36 +00:00
PersonalAccessToken::findToken($plainTextToken)?->delete();
}
2020-09-06 21:11:48 +00:00
public function getUserFromPlainTextToken(string $plainTextToken): ?User
{
2022-07-27 15:32:36 +00:00
return PersonalAccessToken::findToken($plainTextToken)?->tokenable;
2020-09-06 21:11:48 +00:00
}
public function refreshToken(User $user): NewAccessToken
{
$this->destroyTokens($user);
return $this->createToken($user);
}
2020-09-06 18:21:39 +00:00
}