-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
helpers.php
74 lines (62 loc) · 1.76 KB
/
helpers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Cookie;
use Ninja\DeviceTracker\Contracts\StorableId;
use Ninja\DeviceTracker\Enums\DeviceTransport;
use Ninja\DeviceTracker\Enums\SessionTransport;
use Ninja\DeviceTracker\Models\Device;
if (! function_exists('fingerprint')) {
function fingerprint(): ?string
{
if (config('devices.fingerprinting_enabled') === true) {
$cookie = Config::get('devices.fingerprint_id_cookie_name');
$fingerprint = Cookie::get($cookie);
if (is_string($fingerprint)) {
return $fingerprint;
}
}
return null;
}
}
if (! function_exists('device_uuid')) {
function device_uuid(): ?StorableId
{
return DeviceTransport::current()?->get();
}
}
if (! function_exists('session_uuid')) {
function session_uuid(): ?StorableId
{
return SessionTransport::current()?->get();
}
}
if (! function_exists('device')) {
function device(bool $cached = true): ?Device
{
if (config('devices.fingerprinting_enabled') === true) {
$fingerprint = fingerprint();
if ($fingerprint !== null) {
return Device::byFingerprint($fingerprint, $cached);
}
}
$id = device_uuid();
if ($id === null) {
return null;
}
return Device::byUuid($id, $cached);
}
}
if (! function_exists('guard')) {
function guard(): Guard
{
return auth(config('devices.auth_guard'));
}
}
if (! function_exists('user')) {
function user(): ?Authenticatable
{
return guard()->hasUser() ? guard()->user() : null;
}
}