diff --git a/src/Events/AccountFreezed.php b/src/Events/AccountFreezed.php deleted file mode 100644 index 0caa7e2..0000000 --- a/src/Events/AccountFreezed.php +++ /dev/null @@ -1,37 +0,0 @@ -user = $user; - } - - /** - * Get the channels the event should broadcast on. - * - * @return array - */ - public function broadcastOn(): array - { - return [ - //new PrivateChannel('channel-name'), - ]; - } -} diff --git a/src/Events/AccountFrozen.php b/src/Events/AccountFrozen.php new file mode 100644 index 0000000..ff89fe0 --- /dev/null +++ b/src/Events/AccountFrozen.php @@ -0,0 +1,51 @@ + $this->user->name ?? '', + '__account_mobile__' => $this->user->mobile ?? '', + '__account_email__' => $this->user->email ?? '', + '__password_attempt_count__' => $this->user->wrong_password ?? '', + '__account_status__' => $this->user->status ?? '', + '__password_attempt_limit__' => config('fintech.auth.password_threshold', 10), + ]; + } + + /** + * Create a new event instance. + */ + public function __construct($user) + { + $this->user = $user; + } +} diff --git a/src/Exceptions/AccountFreezeException.php b/src/Exceptions/AccountFrozenException.php similarity index 59% rename from src/Exceptions/AccountFreezeException.php rename to src/Exceptions/AccountFrozenException.php index cbafbf4..5336179 100644 --- a/src/Exceptions/AccountFreezeException.php +++ b/src/Exceptions/AccountFrozenException.php @@ -4,7 +4,7 @@ use Exception; -class AccountFreezeException extends Exception +class AccountFrozenException extends Exception { // } diff --git a/src/Models/User.php b/src/Models/User.php index c45f412..cb6e4d8 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -4,6 +4,7 @@ use Fintech\Auth\Traits\TransactionRelations; use Fintech\Core\Traits\AuditableTrait; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\SoftDeletes; @@ -20,6 +21,8 @@ * Class User * @package Fintech\Auth\Models * @method getTeamIdFromToken() + * @property Collection $tokens + * @property int $wrong_password */ class User extends Authenticatable implements HasMedia { diff --git a/src/Providers/EventServiceProvider.php b/src/Providers/EventServiceProvider.php index 56d65a0..0eb8dbb 100644 --- a/src/Providers/EventServiceProvider.php +++ b/src/Providers/EventServiceProvider.php @@ -2,7 +2,7 @@ namespace Fintech\Auth\Providers; -use Fintech\Auth\Events\AccountFreezed; +use Fintech\Auth\Events\AccountFrozen; use Fintech\Auth\Events\AddToFavouriteAccepted; use Fintech\Auth\Events\AddToFavouriteRejected; use Fintech\Auth\Events\AddToFavouriteRequested; @@ -12,6 +12,7 @@ use Fintech\Auth\Events\PasswordResetSuccessful; use Fintech\Auth\Events\VerificationRequested; use Fintech\Core\Listeners\TriggerListener; +use Illuminate\Auth\Events\Attempting; use Illuminate\Auth\Events\Lockout; use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider; @@ -23,6 +24,9 @@ class EventServiceProvider extends ServiceProvider * @var array> */ protected $listen = [ + Attempting::class => [ + TriggerListener::class + ], Lockout::class => [ TriggerListener::class ], @@ -32,7 +36,7 @@ class EventServiceProvider extends ServiceProvider PasswordResetSuccessful::class => [ TriggerListener::class ], - AccountFreezed::class => [ + AccountFrozen::class => [ TriggerListener::class ], LoggedIn::class => [ diff --git a/src/Services/UserService.php b/src/Services/UserService.php index 23261f3..f876b78 100644 --- a/src/Services/UserService.php +++ b/src/Services/UserService.php @@ -3,10 +3,10 @@ namespace Fintech\Auth\Services; use Exception; -use Fintech\Auth\Events\AccountFreezed; +use Fintech\Auth\Events\AccountFrozen; use Fintech\Auth\Events\LoggedIn; use Fintech\Auth\Exceptions\AccessForbiddenException; -use Fintech\Auth\Exceptions\AccountFreezeException; +use Fintech\Auth\Exceptions\AccountFrozenException; use Fintech\Auth\Facades\Auth; use Fintech\Auth\Interfaces\ProfileRepository; use Fintech\Auth\Interfaces\UserRepository; @@ -15,6 +15,10 @@ use Fintech\Core\Enums\Auth\PasswordResetOption; use Fintech\Core\Enums\Auth\UserStatus; use Fintech\MetaData\Facades\MetaData; +use Illuminate\Auth\Events\Attempting; +use Illuminate\Auth\Events\Authenticated; +use Illuminate\Auth\Events\Failed; +use Illuminate\Auth\Events\OtherDeviceLogout; use Illuminate\Foundation\Auth\User; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\DB; @@ -215,7 +219,7 @@ public function reset($user, $field) /** * @param array $inputs * @param string $guard - * @return User|BaseModel|null + * @return BaseModel|\Fintech\Auth\Models\User|User|null * @throws Exception */ public function login(array $inputs, string $guard = 'web') @@ -235,9 +239,14 @@ public function login(array $inputs, string $guard = 'web') Auth::loginAttempt()->create($this->loginAttemptData(null, LoginStatus::Invalid, __('auth::messages.failed'))); + event(new Attempting($guard, $inputs, false)); + throw new Exception(__('auth::messages.failed')); } + /** + * @var \Fintech\Auth\Models\User $attemptUser + */ $attemptUser = $attemptUser->first(); if ($attemptUser->wrong_password > config('fintech.auth.password_threshold', 10)) { @@ -246,14 +255,13 @@ public function login(array $inputs, string $guard = 'web') 'status' => UserStatus::Suspended->value, ]); - event(new AccountFreezed($attemptUser)); + event(new AccountFrozen($attemptUser)); Auth::loginAttempt()->create($this->loginAttemptData($attemptUser->getKey(), LoginStatus::Banned, __('auth::messages.lockup'))); - throw new AccountFreezeException(__('auth::messages.lockup')); + throw new AccountFrozenException(__('auth::messages.lockup')); } - if (!Hash::check($password, $attemptUser->{$passwordField})) { $wrongPasswordCount = $attemptUser->wrong_password + 1; @@ -273,6 +281,8 @@ public function login(array $inputs, string $guard = 'web') ) ); + event(new Failed($guard, $attemptUser, $inputs)); + throw new Exception(__('auth::messages.warning', [ 'attempt' => $wrongPasswordCount, 'threshold' => config('fintech.auth.threshold.password', 10), @@ -281,7 +291,12 @@ public function login(array $inputs, string $guard = 'web') \Illuminate\Support\Facades\Auth::guard($guard)->login($attemptUser); - $attemptUser->tokens->each(fn ($token) => $token->delete()); + if ($attemptUser->tokens->isNotEmpty()) { + + $attemptUser->tokens->each(fn($token) => $token->delete()); + + event(new OtherDeviceLogout($guard, $attemptUser)); + } if (!$attemptUser->can('auth.login')) { @@ -301,8 +316,6 @@ public function login(array $inputs, string $guard = 'web') throw new AccessForbiddenException(__('auth::messages.forbidden', ['permission' => permission_format('auth.login', 'auth')])); } - event(new LoggedIn($attemptUser)); - Auth::loginAttempt()->create( $this->loginAttemptData( $attemptUser->getKey(), @@ -311,6 +324,10 @@ public function login(array $inputs, string $guard = 'web') ) ); + event(new LoggedIn($attemptUser)); + + event(new Authenticated($guard, $attemptUser)); + return $attemptUser; }