Skip to content

Commit

Permalink
LP-3 feat: ip verification middleware added
Browse files Browse the repository at this point in the history
  • Loading branch information
hafijul233 committed Oct 1, 2023
1 parent 992dd1a commit 9b23b81
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 4 deletions.
3 changes: 1 addition & 2 deletions lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
'failed' => 'These credentials do not match our records.',
'password' => 'The provided password is incorrect.',
'throttle' => 'Too many login attempts. Please try again in :seconds seconds.',
'Invalid Token' => 'Invalid Token',
'Your IP :user_ip is blocked. Please contact support.' => 'Your IP :user_ip is blocked. Please contact support.', //don't translate :user_ip
'ip_blocked' => 'Your IP :ip is blocked, Please contact support.',
'This user are not login. Please contact support.' => 'This user are not login. Please contact support.',
'Sorry, You entered wrong mobile number or invalid password!' => 'Sorry, You entered wrong mobile number or invalid password!',
'warning' => 'Sorry, You entered wrong credentials! You already attempt :attempt. times out of :threshold',
Expand Down
2 changes: 2 additions & 0 deletions src/Http/Controllers/AuthenticatedSessionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function store(LoginRequest $request): LoginResource|JsonResponse

$request->clearRateLimited();

if ($attemptUser->hasPermission())

Auth::login($attemptUser);

Auth::user()->tokens->each(fn($token) => $token->delete());
Expand Down
4 changes: 3 additions & 1 deletion src/Http/Middleware/EnsureEmailIsVerified.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class EnsureEmailIsVerified
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
* @param Request $request
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
* @return Response
*/
public function handle(Request $request, Closure $next): Response
{
Expand Down
31 changes: 31 additions & 0 deletions src/Http/Middleware/IpAddressVerified.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Fintech\Auth\Http\Middlewares;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Symfony\Component\HttpFoundation\Response;
use Fintech\Core\Traits\ApiResponseTrait;

class IpAddressVerified
{
use ApiResponseTrait;

/**
* Handle an incoming request.
*
* @param Request $request
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
* @return Response
*/
public function handle(Request $request, Closure $next): Response
{
if (App::isProduction()) {

return $this->banned(__('auth::messages.ip_blocked', ['ip' => $request->ip()]));
}

return $next($request);
}
}
2 changes: 1 addition & 1 deletion src/Http/Requests/LoginRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function ensureIsNotRateLimited(): void

$seconds = RateLimiter::availableIn($this->throttleKey());

abort(Response::HTTP_TOO_MANY_REQUESTS, trans('auth.throttle', [
abort(Response::HTTP_TOO_MANY_REQUESTS, trans('auth::messages.throttle', [
'seconds' => $seconds,
'minutes' => ceil($seconds / 60),
]));
Expand Down
2 changes: 2 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
use Illuminate\Foundation\Auth\User as Authenticatable;
use Laravel\Sanctum\HasApiTokens;
use OwenIt\Auditing\Contracts\Auditable;
use Spatie\Permission\Traits\HasRoles;

class User extends Authenticatable implements Auditable
{
use BlameableTrait;
use HasApiTokens;
use \OwenIt\Auditing\Auditable;
use SoftDeletes;
use HasRoles;

/*
|--------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions src/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Fintech\Auth;

use Fintech\Auth\Http\Middlewares\IpAddressVerified;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
Expand All @@ -27,6 +28,8 @@ public function boot()
->middleware('api')
->group(__DIR__.'/../routes/api.php');
});

Route::middlewareGroup('ip_verified', [IpAddressVerified::class]);
}

/**
Expand Down

0 comments on commit 9b23b81

Please sign in to comment.