-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJWTAuthProvider.php
More file actions
50 lines (40 loc) · 1.28 KB
/
JWTAuthProvider.php
File metadata and controls
50 lines (40 loc) · 1.28 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
<?php
namespace App\Http\Helpers;
use App\Models\User;
use PHPOpenSourceSaver\JWTAuth\JWTAuth;
use PHPOpenSourceSaver\JWTAuth\Providers\Auth\Illuminate as JWTAuthIlluminate;
class JWTAuthProvider extends JWTAuthIlluminate
{
/**
* @param mixed $id
* @return bool
*/
public function byId($id)
{
// Be compatible to other user loading methods
if (parent::byId($id)) {
return true;
}
/** @var $user */
$user = User::whereEmail($id)->first();
if (!$user) {
// Could also return false if user should not be created
/** @var JWTAuth $auth */
$auth = app('tymon.jwt.auth');
// Load payload data
$payload = $auth->getPayload();
// Create user
(new User([
'email' => $payload->get('sub'), // or $payload->get('profile')['email'] or ->get('user') when available
'name' => $payload->get('profile')['name'],
'password' => ''
]))->save();
// Load created users data
$user = User::whereEmail($id)->first();
}
// Log in the user for the request
$this->auth->setUser($user);
// User is authorized
return true;
}
}