Skip to content

Commit

Permalink
code optimization added
Browse files Browse the repository at this point in the history
  • Loading branch information
hafijul233 committed Sep 5, 2024
1 parent 130a00d commit 31e073d
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 27 deletions.
27 changes: 18 additions & 9 deletions config/auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// config for fintech/auth
use Fintech\Auth\Models\Audit;
use Fintech\Auth\Models\Favourite;
use Fintech\Auth\Models\LoginAttempt;
use Fintech\Auth\Models\OneTimePin;
use Fintech\Auth\Models\Permission;
use Fintech\Auth\Models\Profile;
Expand All @@ -18,6 +19,14 @@
use Fintech\Auth\Repositories\Eloquent\RoleRepository;
use Fintech\Auth\Repositories\Eloquent\TeamRepository;
use Fintech\Auth\Repositories\Eloquent\UserRepository;
use Fintech\Auth\Services\Vendors\GeoIp\Cloudflare;
use Fintech\Auth\Services\Vendors\GeoIp\Ip2Location;
use Fintech\Auth\Services\Vendors\GeoIp\IpApi;
use Fintech\Auth\Services\Vendors\GeoIp\IpData;
use Fintech\Auth\Services\Vendors\GeoIp\IpInfo;
use Fintech\Auth\Services\Vendors\GeoIp\Kloudend;
use Fintech\Auth\Services\Vendors\GeoIp\Local;
use Fintech\Auth\Services\Vendors\GeoIp\MaxMind;

return [

Expand Down Expand Up @@ -147,7 +156,7 @@
|
| This value will be used to across system where model is needed
*/
'login_attempt_model' => \Fintech\Auth\Models\LoginAttempt::class,
'login_attempt_model' => LoginAttempt::class,
'record_login_attempt' => env('PACKAGE_AUTH_RECORD_LOGIN_ATTEMPT', false),
/*
|--------------------------------------------------------------------------
Expand All @@ -160,35 +169,35 @@
'default' => env('PACKAGE_AUTH_GEOIP_DRIVER', null),
'drivers' => [
'local' => [
'class' => \Fintech\Auth\Services\Vendors\GeoIp\Local::class,
'class' => Local::class,
'type' => 'city',
'path' => 'maxmind/GeoLite2-City.mmdb',
],
'ipapi' => [
'class' => \Fintech\Auth\Services\Vendors\GeoIp\IpApi::class,
'class' => IpApi::class,
'token' => env('PACKAGE_AUTH_IPAPI_TOKEN'),
],
'ipinfo' => [
'class' => \Fintech\Auth\Services\Vendors\GeoIp\IpInfo::class,
'class' => IpInfo::class,
'token' => env('PACKAGE_AUTH_IPINFO_TOKEN'),
],
'ipdata' => [
'class' => \Fintech\Auth\Services\Vendors\GeoIp\IpData::class,
'class' => IpData::class,
'token' => env('PACKAGE_AUTH_IPDATA_TOKEN'),
],
'ip2location' => [
'class' => \Fintech\Auth\Services\Vendors\GeoIp\Ip2Location::class,
'class' => Ip2Location::class,
'token' => env('PACKAGE_AUTH_IP2LOCATION_TOKEN'),
],
'cloudflare' => [
'class' => \Fintech\Auth\Services\Vendors\GeoIp\Cloudflare::class,
'class' => Cloudflare::class,
],
'kloudend' => [
'class' => \Fintech\Auth\Services\Vendors\GeoIp\Kloudend::class,
'class' => Kloudend::class,
'token' => env('PACKAGE_AUTH_KLOUDEND_TOKEN'),
],
'maxmind' => [
'class' => \Fintech\Auth\Services\Vendors\GeoIp\MaxMind::class,
'class' => MaxMind::class,
'user_id' => env('PACKAGE_AUTH_MAXMIND_USER_ID'),
'license_key' => env('PACKAGE_AUTH_MAXMIND_LICENSE_KEY'),
'options' => ['host' => 'geoip.maxmind.com'],
Expand Down
3 changes: 2 additions & 1 deletion src/Interfaces/LoginAttemptRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Fintech\Core\Abstracts\BaseModel;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Support\Collection;
use InvalidArgumentException;

/**
* Interface LoginAttemptRepository
Expand Down Expand Up @@ -48,7 +49,7 @@ public function delete(int|string $id);
* find and restore a entry from records
*
* @param int|string $id
* @throws \InvalidArgumentException
* @throws InvalidArgumentException
*/
public function restore(int|string $id);
}
3 changes: 2 additions & 1 deletion src/Models/LoginAttempt.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fintech\Core\Abstracts\BaseModel;
use Fintech\Core\Traits\AuditableTrait;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class LoginAttempt extends BaseModel
Expand Down Expand Up @@ -37,7 +38,7 @@ class LoginAttempt extends BaseModel
| RELATIONS
|--------------------------------------------------------------------------
*/
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
public function user(): BelongsTo
{
return $this->belongsTo(config('fintech.auth.user_model', User::class), 'user_id');
}
Expand Down
3 changes: 2 additions & 1 deletion src/Notifications/OTPNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fintech\Auth\Notifications;

use Fintech\Auth\Models\User;
use Fintech\Core\Enums\Auth\OTPOption;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Messages\MailMessage;
Expand Down Expand Up @@ -40,7 +41,7 @@ public function via(object $notifiable): array
}

if (request()->filled('user')) {
$userModel = config('fintech.auth.user_model', \Fintech\Auth\Models\User::class);
$userModel = config('fintech.auth.user_model', User::class);
return ($notifiable instanceof $userModel)
? $notifiable->prefer ? [$notifiable->prefer] : ['mail']
: ['database'];
Expand Down
5 changes: 3 additions & 2 deletions src/Providers/RepositoryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\ServiceProvider;
use InvalidArgumentException;

class RepositoryServiceProvider extends ServiceProvider implements DeferrableProvider
{
Expand All @@ -25,7 +26,7 @@ public function register(): void
if ($current = config('fintech.auth.geoip.default')) {

if (!config("fintech.auth.geoip.drivers.{$current}")) {
throw new \InvalidArgumentException("No driver configuration found named `{$current}`.");
throw new InvalidArgumentException("No driver configuration found named `{$current}`.");
}

$config = config("fintech.auth.geoip.drivers.{$current}");
Expand All @@ -37,7 +38,7 @@ public function register(): void
return new $class($config);

} else {
throw new \InvalidArgumentException("No driver is assigned for GeoIP Service.");
throw new InvalidArgumentException("No driver is assigned for GeoIP Service.");
}
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/Repositories/Eloquent/LoginAttemptRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Fintech\Auth\Repositories\Eloquent;

use Fintech\Auth\Interfaces\LoginAttemptRepository as InterfacesLoginAttemptRepository;
use Fintech\Auth\Models\LoginAttempt;
use Fintech\Core\Repositories\EloquentRepository;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Collection;
Expand All @@ -15,7 +16,7 @@ class LoginAttemptRepository extends EloquentRepository implements InterfacesLog
{
public function __construct()
{
parent::__construct(config('fintech.auth.login_attempt_model', \Fintech\Auth\Models\LoginAttempt::class));
parent::__construct(config('fintech.auth.login_attempt_model', LoginAttempt::class));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Repositories/Mongodb/LoginAttemptRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Fintech\Auth\Repositories\Mongodb;

use Fintech\Auth\Interfaces\LoginAttemptRepository as InterfacesLoginAttemptRepository;
use Fintech\Auth\Models\LoginAttempt;
use Fintech\Core\Repositories\MongodbRepository;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Collection;
Expand All @@ -15,7 +16,7 @@ class LoginAttemptRepository extends MongodbRepository implements InterfacesLogi
{
public function __construct()
{
parent::__construct(config('fintech.auth.login_attempt_model', \Fintech\Auth\Models\LoginAttempt::class));
parent::__construct(config('fintech.auth.login_attempt_model', LoginAttempt::class));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Services/PasswordResetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ class PasswordResetService
public function __construct(
private readonly OneTimePinRepository $oneTimePinRepository,
private readonly UserRepository $userRepository
) {
)
{
$this->passwordField = config('fintech.auth.password_field', 'password');

$this->resetMethod = config('fintech.auth.password_reset_method', PasswordResetOption::ResetLink->value);
Expand Down
3 changes: 2 additions & 1 deletion src/Services/PinResetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ class PinResetService
public function __construct(
private readonly OneTimePinRepository $oneTimePinRepository,
private readonly UserRepository $userRepository
) {
)
{
$this->pinField = config('fintech.auth.pin_field', 'pin');

$this->resetMethod = config('fintech.auth.password_reset_method', PasswordResetOption::ResetLink->value);
Expand Down
3 changes: 2 additions & 1 deletion src/Services/ProfileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class ProfileService
*/
public function __construct(
private readonly ProfileRepository $profileRepository
) {
)
{
}

public function create(string|int $user_id, array $inputs = [])
Expand Down
10 changes: 6 additions & 4 deletions src/Services/UserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use PDOException;
use stdClass;

/**
* Class UserService
Expand All @@ -41,7 +42,8 @@ class UserService
public function __construct(
private readonly UserRepository $userRepository,
private readonly ProfileRepository $profileRepository
) {
)
{
$this->loginAttempt = [];
}

Expand Down Expand Up @@ -298,7 +300,7 @@ public function login(array $inputs, string $guard = 'web')

if ($attemptUser->tokens->isNotEmpty()) {

$attemptUser->tokens->each(fn ($token) => $token->delete());
$attemptUser->tokens->each(fn($token) => $token->delete());

event(new OtherDeviceLogout($guard, $attemptUser));
}
Expand Down Expand Up @@ -363,8 +365,8 @@ private function loginAttemptData($user_id, $status, $note): array
$ipAddress['continent_name']]);

$country = MetaData::country()->list(['iso2' => $ipAddress['country_code']])->first();
$state = new \stdClass();
$city = new \stdClass();
$state = new stdClass();
$city = new stdClass();

if ($country) {
$state = MetaData::state()->list(['country_id' => $country->id, 'search' => $ipAddress['region_name']])->first();
Expand Down
3 changes: 2 additions & 1 deletion src/Services/Vendors/GeoIp/IpApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Fintech\Auth\Interfaces\GeoIp;
use Illuminate\Support\Facades\Http;
use InvalidArgumentException;

class IpApi implements GeoIp
{
Expand All @@ -17,7 +18,7 @@ class IpApi implements GeoIp
public function __construct(array $config = [])
{
if (!$config['token']) {
throw new \InvalidArgumentException("IP API Access Key is missing.");
throw new InvalidArgumentException("IP API Access Key is missing.");
}

$this->token = $config['token'];
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/LoginTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php

use Fintech\Auth\Facades\Auth;

use function Pest\Laravel\postJson;

test('login failed', function () {
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/PermissionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Fintech\Auth\Facades\Auth;
use Illuminate\Support\Str;

use function Pest\Laravel\deleteJson;
use function Pest\Laravel\getJson;
use function Pest\Laravel\postJson;
Expand Down
1 change: 0 additions & 1 deletion tests/Feature/RoleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use Fintech\Auth\Facades\Auth;
use Illuminate\Support\Str;

use function Pest\Laravel\deleteJson;
use function Pest\Laravel\getJson;
use function Pest\Laravel\postJson;
Expand Down

0 comments on commit 31e073d

Please sign in to comment.