Skip to content

Commit

Permalink
Merge pull request #14 from diego-ninja/feature/remove_laravel_sessio…
Browse files Browse the repository at this point in the history
…n_use

feature: removes the coupling with laravel session
  • Loading branch information
diego-ninja authored Nov 21, 2024
2 parents 4873a1c + 0c6d89f commit 14b87ee
Show file tree
Hide file tree
Showing 124 changed files with 674 additions and 1,689 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"larastan/larastan": "^2.0",
"mockery/mockery": "^1.4.4",
"pestphp/pest": "^2 || ^3",
"squizlabs/php_codesniffer": "^3.7",
"laravel/octane": "^2.5",
"swoole/ide-helper": "~5.0.0",
"geoip2/geoip2": "^3.0"
"geoip2/geoip2": "^3.0",
"laravel/pint": "^1.18"
},
"minimum-stability": "stable",
"autoload": {
Expand Down
36 changes: 18 additions & 18 deletions config/devices.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,45 @@

/*
|--------------------------------------------------------------------------
| Cookie name for current user device tracking
| Parameter name for current user device tracking
|--------------------------------------------------------------------------
| This option specifies the name of the cookie that will be used to store
| This option specifies the name of the cookie that will be used to transport
| the device id of the current user.
|
*/
'device_id_cookie_name' => 'laravel_device_id',
'device_id_parameter' => 'laravel_device_id',

/*
|--------------------------------------------------------------------------
| Request param for device id
| Transport for device id
|--------------------------------------------------------------------------
| This option specifies the name of the request parameter that will be used to store
| the device uuid during the request.
| This option specifies the transport method for the device id.
|
| Options: 'cookie', 'header', 'session'
|
*/
'device_id_request_param' => 'laravel_device_id',
'device_id_transport' => 'cookie',

/*
|--------------------------------------------------------------------------
| Header name for device id
| Parameter name for current user session tracking
|--------------------------------------------------------------------------
| This option specifies the name of the header that will be used to store
| the device uuid during the request.
| This option specifies the name of the parameter that will be used to transport
| the session id for the current user.
|
*/
'device_id_header_name' => 'X-Device',
'session_id_parameter' => 'laravel_session_id',

/*
|--------------------------------------------------------------------------
| Transport for device id
| Transport for session id
|--------------------------------------------------------------------------
| This option specifies the transport method for the device id.
| This option specifies the transport method for the session id.
|
| Options: 'cookie', 'header'
| Options: 'cookie', 'header', 'session'
|
*/
'device_id_transport' => 'cookie',
'session_id_transport' => 'cookie',

/*
|--------------------------------------------------------------------------
Expand Down Expand Up @@ -232,7 +233,6 @@
*/
'client_fingerprint_transport' => 'cookie',


/*
|--------------------------------------------------------------------------
| Fingerprint key
Expand Down Expand Up @@ -452,7 +452,7 @@
| used to test the package in development mode.
|
*/
'development_ip_pool' => ['138.100.56.25','2.153.101.169','104.26.14.39','104.26.3.12'],
'development_ip_pool' => ['138.100.56.25', '2.153.101.169', '104.26.14.39', '104.26.3.12'],

/*
|--------------------------------------------------------------------------
Expand All @@ -472,6 +472,6 @@
'Mozilla/7.0 (iPhone; CPU iPhone OS 18_7; iPhone 15 Pro Max) AppleWebKit/533.2 (KHTML, seperti Gecko) CriOS/432.0.8702.51 Seluler/15E148 Safari/804.17',
'Mozilla/7.0 (iPhone; CPU iPhone OS 18_7; iPhone 15 Pro Max) AppleWebKit/533.2 (KHTML, like Gecko) CriOS/432.0.8702.51 Mobile/15E148 Safari/804.17',
'Mozilla/5.0 (Linux; Android 13; 2211133G Build/TKQ1.220905.001; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/113.0.5672.76 Mobile Safari/537.36',
'Mozilla/5.0 (Linux; U; Android 13; pl-pl; Xiaomi 13 Build/TKQ1.220905.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/100.0.4896.127 Mobile Safari/537.36 XiaoMi/MiuiBrowser/13.28.0-gn'
'Mozilla/5.0 (Linux; U; Android 13; pl-pl; Xiaomi 13 Build/TKQ1.220905.001) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/100.0.4896.127 Mobile Safari/537.36 XiaoMi/MiuiBrowser/13.28.0-gn',
],
];
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Ninja\DeviceTracker\Enums\SessionStatus;

return new class extends Migration {
return new class extends Migration
{
public function up(): void
{
Schema::create('device_sessions', function (Blueprint $table) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Ninja\DeviceTracker\Enums\DeviceStatus;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
use Illuminate\Support\Facades\Schema;
use Ninja\DeviceTracker\Enums\DeviceStatus;

return new class extends Migration {
return new class extends Migration
{
public function up(): void
{
Schema::create($this->table(), function (Blueprint $table) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Ninja\DeviceTracker\Enums\SessionStatus;

return new class extends Migration {
return new class extends Migration
{
public function up(): void
{
Schema::create("google_2fa", function (Blueprint $table) {
Schema::create('google_2fa', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->boolean('enabled')->nullable();
Expand All @@ -20,6 +20,6 @@ public function up(): void

public function down(): void
{
Schema::drop("google_2fa");
Schema::drop('google_2fa');
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

return new class extends Migration {
return new class extends Migration
{
public function up(): void
{
Schema::table('devices', function (Blueprint $table) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

return new class extends Migration {
return new class extends Migration
{
public function up(): void
{
Schema::table('device_sessions', function (Blueprint $table) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
public function up(): void
{
Schema::create('device_events', function (Blueprint $table) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
return new class extends Migration
{
public function up(): void
{
Schema::table('devices', function (Blueprint $table) {
Expand Down
28 changes: 7 additions & 21 deletions helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@

use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Session as SessionFacade;
use Ninja\DeviceTracker\Contracts\StorableId;
use Ninja\DeviceTracker\Enums\Transport;
use Ninja\DeviceTracker\Exception\SessionNotFoundException;
use Ninja\DeviceTracker\Factories\SessionIdFactory;
use Ninja\DeviceTracker\Enums\DeviceTransport;
use Ninja\DeviceTracker\Enums\SessionTransport;
use Ninja\DeviceTracker\Models\Device;
use Ninja\DeviceTracker\Models\Session;

if (! function_exists('fingerprint')) {
function fingerprint(): ?string
{
if (Config::get('devices.fingerprinting_enabled')) {
$cookie = Config::get('devices.fingerprint_id_cookie_name');

return Cookie::has($cookie) ? Cookie::get($cookie) : null;
}

Expand All @@ -25,27 +23,14 @@ function fingerprint(): ?string
if (! function_exists('device_uuid')) {
function device_uuid(): ?StorableId
{
return Transport::current()->get();
return DeviceTransport::current()->get();
}
}

if (! function_exists('session_uuid')) {
function session_uuid(): ?StorableId
{
$id = SessionFacade::get(Session::DEVICE_SESSION_ID);
return $id ? SessionIdFactory::from($id) : null;
}
}

if (! function_exists('session')) {
function session(): ?Session
{
try {
$id = session_uuid();
return $id ? Session::byUuid($id) : null;
} catch (SessionNotFoundException) {
return null;
}
return SessionTransport::current()->get();
}
}

Expand All @@ -61,6 +46,7 @@ function device(bool $cached = true): ?Device
}

$id = device_uuid();

return $id ? Device::byUuid($id, $cached) : null;
}
}
}
Loading

0 comments on commit 14b87ee

Please sign in to comment.