Browse Source

new device login notification feature added to auth

mahdi
mahdihty 4 years ago
parent
commit
c7aa20d9d3
  1. 29
      app/Http/Controllers/AuthController.php
  2. 2
      routes/api.php

29
app/Http/Controllers/AuthController.php

@ -4,6 +4,7 @@ namespace App\Http\Controllers;
use App\Models\Business; use App\Models\Business;
use App\Models\User; use App\Models\User;
use App\Notifications\DBNotification;
use App\Notifications\MailNotification; use App\Notifications\MailNotification;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@ -99,6 +100,9 @@ class AuthController extends Controller
if ($user && Hash::check($request->password, $user->password)) { if ($user && Hash::check($request->password, $user->password)) {
Auth::setUser($user); Auth::setUser($user);
// for new device login
$this->loginNotif($this->firstOrNot());
return [ return [
'auth' => $this->createFingerPrint(), 'auth' => $this->createFingerPrint(),
'businesses' => Auth::user()->businesses->keyBy('id')->map(fn($b, $bid) => Business::info($bid)) '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); 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',
]));
}
}
} }

2
routes/api.php

@ -32,7 +32,7 @@ $router->group(['prefix' => 'auth'], function () use ($router) {
$router->post('verification', 'AuthController@verification')->name('verification'); $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/redirect', 'AuthController@redirectToGoogle')->name('google.redirect');
$router->get('google/callback', 'AuthController@handleGoogleCallback')->name('google.callback'); $router->get('google/callback', 'AuthController@handleGoogleCallback')->name('google.callback');

Loading…
Cancel
Save