mirror of
https://gitlab.com/izyim/prototypes/ddd-example.git
synced 2026-08-19 20:44:07 +00:00
47 lines
1.3 KiB
PHP
47 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Src\Auth\Application;
|
|
|
|
use Illuminate\Auth\AuthenticationException;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Src\Zone\User\Application\Mappers\UserMapper;
|
|
use Src\Zone\User\Domain\Model\User;
|
|
use Src\Zone\User\Infrastructure\EloquentModels\UserEloquentModel;
|
|
use Src\Auth\Domain\AuthInterface;
|
|
use Tymon\JWTAuth\Exceptions\JWTException;
|
|
use Tymon\JWTAuth\Facades\JWTAuth as TymonJWTAuth;
|
|
|
|
class JWTAuth implements AuthInterface
|
|
{
|
|
|
|
public function login(array $credentials): string
|
|
{
|
|
$user = UserEloquentModel::query()->where('email', $credentials['email'])->first();
|
|
if (!$user || !$user->is_active) {
|
|
throw new AuthenticationException();
|
|
} elseif (!$token = auth()->attempt($credentials)) {
|
|
throw new AuthenticationException();
|
|
}
|
|
return $token;
|
|
}
|
|
|
|
public function logout(): void
|
|
{
|
|
auth()->logout();
|
|
}
|
|
|
|
public function me(): User
|
|
{
|
|
return UserMapper::fromAuth(auth()->user());
|
|
}
|
|
|
|
public function refresh(): string
|
|
{
|
|
try {
|
|
return TymonJWTAuth::parseToken()->refresh();
|
|
} catch (JWTException $e) {
|
|
Log::error($e->getMessage());
|
|
throw new AuthenticationException($e->getMessage());
|
|
}
|
|
}
|
|
} |