2023-08-20 22:35:58 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Exceptions\InvalidCredentialsException;
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Repositories\UserRepository;
|
2024-01-09 23:26:16 +00:00
|
|
|
use App\Values\CompositeToken;
|
2024-02-25 19:32:53 +00:00
|
|
|
use Illuminate\Auth\Events\PasswordReset;
|
|
|
|
use Illuminate\Auth\Passwords\PasswordBroker;
|
2023-08-20 22:35:58 +00:00
|
|
|
use Illuminate\Hashing\HashManager;
|
2024-04-03 14:48:52 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2024-02-25 19:32:53 +00:00
|
|
|
use Illuminate\Support\Facades\Password;
|
2023-08-20 22:35:58 +00:00
|
|
|
|
|
|
|
class AuthenticationService
|
|
|
|
{
|
|
|
|
public function __construct(
|
2024-04-18 14:36:28 +00:00
|
|
|
private readonly UserRepository $userRepository,
|
|
|
|
private readonly TokenManager $tokenManager,
|
|
|
|
private readonly HashManager $hash,
|
|
|
|
private readonly PasswordBroker $passwordBroker
|
2023-08-20 22:35:58 +00:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2024-01-09 23:26:16 +00:00
|
|
|
public function login(string $email, string $password): CompositeToken
|
2023-08-20 22:35:58 +00:00
|
|
|
{
|
|
|
|
/** @var User|null $user */
|
|
|
|
$user = $this->userRepository->getFirstWhere('email', $email);
|
|
|
|
|
|
|
|
if (!$user || !$this->hash->check($password, $user->password)) {
|
|
|
|
throw new InvalidCredentialsException();
|
|
|
|
}
|
|
|
|
|
2023-10-10 15:19:00 +00:00
|
|
|
if ($this->hash->needsRehash($user->password)) {
|
|
|
|
$user->password = $this->hash->make($password);
|
|
|
|
$user->save();
|
|
|
|
}
|
|
|
|
|
2024-03-30 16:49:25 +00:00
|
|
|
return $this->logUserIn($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function logUserIn(User $user): CompositeToken
|
|
|
|
{
|
2024-01-09 23:26:16 +00:00
|
|
|
return $this->tokenManager->createCompositeToken($user);
|
2023-08-20 22:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function logoutViaBearerToken(string $token): void
|
|
|
|
{
|
|
|
|
$this->tokenManager->deleteCompositionToken($token);
|
|
|
|
}
|
2024-02-25 19:32:53 +00:00
|
|
|
|
|
|
|
public function trySendResetPasswordLink(string $email): bool
|
|
|
|
{
|
|
|
|
return $this->passwordBroker->sendResetLink(['email' => $email]) === Password::RESET_LINK_SENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tryResetPasswordUsingBroker(string $email, string $password, string $token): bool
|
|
|
|
{
|
|
|
|
$credentials = [
|
|
|
|
'email' => $email,
|
|
|
|
'password' => $password,
|
|
|
|
'password_confirmation' => $password,
|
|
|
|
'token' => $token,
|
|
|
|
];
|
|
|
|
|
|
|
|
$status = $this->passwordBroker->reset($credentials, function (User $user, string $password): void {
|
|
|
|
$user->password = $this->hash->make($password);
|
|
|
|
$user->save();
|
|
|
|
event(new PasswordReset($user));
|
|
|
|
});
|
|
|
|
|
|
|
|
return $status === Password::PASSWORD_RESET;
|
|
|
|
}
|
2024-04-03 14:48:52 +00:00
|
|
|
|
|
|
|
public function generateOneTimeToken(User $user): string
|
|
|
|
{
|
|
|
|
$token = bin2hex(random_bytes(16));
|
|
|
|
Cache::set("one-time-token.$user->id", $token, 60 * 10);
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
2023-08-20 22:35:58 +00:00
|
|
|
}
|