From c7aa20d9d3ffa5a4a7c81a3e59679347fc8282cb Mon Sep 17 00:00:00 2001 From: mahdihty Date: Sat, 10 Apr 2021 18:16:16 +0430 Subject: [PATCH] new device login notification feature added to auth --- app/Http/Controllers/AuthController.php | 29 +++++++++++++++++++++++++ routes/api.php | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 9b49212..a43a73a 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers; use App\Models\Business; use App\Models\User; +use App\Notifications\DBNotification; use App\Notifications\MailNotification; use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; @@ -99,6 +100,9 @@ class AuthController extends Controller if ($user && Hash::check($request->password, $user->password)) { Auth::setUser($user); + // for new device login + $this->loginNotif($this->firstOrNot()); + return [ 'auth' => $this->createFingerPrint(), 'businesses' => Auth::user()->businesses->keyBy('id')->map(fn($b, $bid) => Business::info($bid)) @@ -252,4 +256,29 @@ class AuthController extends Controller return Auth::user()->fingerprints()->firstOrCreate($attributes, $attributes + $values); } + + public function firstOrNot() + { + return Auth::user()->fingerprints()->where([ + ['agent', '!=',request()->getAgent()], + ['ip', '!=',request()->getClientIp()], + ['os', '!=',request()->getOS()], + ['latitude', '!=',\request()->getLocation()->getAttribute('lat')], + ['longitude', '!=',\request()->getLocation()->getAttribute('lon')], + ])->exists(); + } + + public function loginNotif($send) + { + if ($send) { + Notification::send(Auth::user(), new MailNotification([ + 'greeting' => 'hi', + 'subject' => 'login with another device', + 'body' => 'Warning someone login to your account with new device. check it and dont worry', + ])); + Notification::send(Auth::user(), new DBNotification([ + 'body' => 'Warning someone login to your account with new device. check it and dont worry', + ])); + } + } } diff --git a/routes/api.php b/routes/api.php index cd7d50f..8bca81d 100644 --- a/routes/api.php +++ b/routes/api.php @@ -32,7 +32,7 @@ $router->group(['prefix' => 'auth'], function () use ($router) { $router->post('verification', 'AuthController@verification')->name('verification'); - $router->post('resend', 'AuthController@resendLink')->middleware('throttle:1'); + $router->post('resend', 'AuthController@resendLink')->middleware('throttle:1'); // one request per min $router->get('google/redirect', 'AuthController@redirectToGoogle')->name('google.redirect'); $router->get('google/callback', 'AuthController@handleGoogleCallback')->name('google.callback');