JwtPayloadFormat.php
1.44 KB
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
<?php
namespace App\Support;
use App;
use Hikoon\LaravelJwt\Support\Format;
use Illuminate\Support\Arr;
class JwtPayloadFormat extends Format
{
/**
* @param array<string,mixed> $payload
* @return array<string,mixed>
*/
public function encrypt(array $payload): array
{
$data = $this->getBasicPayload($payload);
if (in_array($this->guard, ['admin', 'manage', 'user'], true)) {
$data['extend'] = ['id' => $data['jti'], ...Arr::get($payload, 'extend', [])];
return $data;
}
if ($this->guard === 'app') {
$data['data'] = ['id' => $data['jti'], 'env' => App::isProduction() ? 'pro' : 'dev', ...Arr::get($payload, 'extend', [])];
return Arr::only($data, ['data', 'iat', 'exp', 'jti']);
}
return $payload;
}
/**
* @param array<string,mixed> $payload
* @return array<string,mixed>
*/
public function decrypt(array $payload): array
{
$data = $this->getBasicPayload($payload);
if (in_array($this->guard, ['admin', 'manage', 'user'], true)) {
$data['jti'] = $data['jti'] ?? Arr::get($payload, 'extend.id', 0);
return $data;
}
if ($this->guard === 'app') {
$data['jti'] = $data['jti'] ?? Arr::get($payload, 'data.id', 0);
$data['extend'] = Arr::get($payload, 'data', []);
return $data;
}
return $payload;
}
}