1. Go to this page and download the library: Download alajusticia/laravel-logins library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alajusticia / laravel-logins example snippets
use ALajusticia\Logins\Traits\HasLogins;
use Illuminate\Foundation\Auth\User as Authenticatable;
// ...
class User extends Authenticatable
{
use HasLogins;
// ...
}
use ALajusticia\Logins\Logins;
use ALajusticia\Logins\RequestContext;
Logins::describeDeviceUsing(function (RequestContext $context): ?array {
if (! $context->tokenName() || ! request()->has('device')) {
return null;
}
return [
'device_type' => request()->input('device.type'), // desktop, mobile or tablet
'device' => request()->input('device.name'), // Apple iPhone 17 Pro
'platform' => request()->input('device.os'), // iOS
'browser' => config('app.name'), // The app, in place of the browser
];
});
use ALajusticia\Logins\Logins;
public function boot(): void
{
Logins::registerRoutes();
}
'auth_session' => null,
$logins = request()->user()->logins;
$currentLogin = request()->user()->current_login;
request()->user()->logout(1); // Revoke the login where id=1
request()->user()->logout(); // Revoke the current login
request()->user()->logoutAll();
request()->user()->logoutOthers();
return request()->ip();
use ALajusticia\Logins\Logins;
use App\Support\ClientIpResolver;
Logins::getIpAddressUsing(function (): ?string {
return app(ClientIpResolver::class)->resolve(request());
});
use ALajusticia\Logins\Events\LoggedIn;
use Illuminate\Support\Facades\Event;
Event::listen(function (LoggedIn $event) {
// Methods available in RequestContext:
$event->context->date(); // Returns the date of the login (Carbon object)
$event->context->userAgent(); // Returns the full, unparsed, User-Agent header
$event->context->ipAddress(); // Returns the client's IP address
$event->context->parser(); // Returns the parser used to parse the User-Agent header
$event->context->location(); // Returns the location (Stevebauman\Location\Position object), if IP address geolocation enabled
$event->context->tokenName(); // Returns the personal access token name (if Sanctum stateless authentication)
// Methods available in the parser:
$this->context->parser()->getDevice(); // The name of the device
$this->context->parser()->getDeviceType(); // The type of the device (desktop, mobile, tablet or phone)
$this->context->parser()->getPlatform(); // The name of the platform/OS
$this->context->parser()->getBrowser(); // The name of the browser
})
use ALajusticia\Logins\Logins;
$token = Logins::withoutNotifications(fn () => $user->createToken('mobile_app_iphone'));