1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of laravel.su package. |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
|
9
|
|
|
namespace App\GraphQL\Queries; |
10
|
|
|
|
11
|
|
|
use App\GraphQL\Serializers\AuthUserSerializer; |
12
|
|
|
use App\Services\TokenAuth; |
13
|
|
|
use Illuminate\Contracts\Auth\Guard; |
14
|
|
|
use Illuminate\Contracts\Auth\StatefulGuard; |
15
|
|
|
use Illuminate\Support\Arr; |
16
|
|
|
use GraphQL\Type\Definition\Type; |
17
|
|
|
use App\GraphQL\Types\AuthUserType; |
18
|
|
|
use GraphQL\Type\Definition\ObjectType; |
19
|
|
|
use App\GraphQL\Serializers\UserSerializer; |
20
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class AuthMutation |
24
|
|
|
* @package App\GraphQL\Mutations |
25
|
|
|
*/ |
26
|
|
|
class AuthQuery extends AbstractQuery |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $attributes = [ |
32
|
|
|
'name' => 'auth', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var TokenAuth |
37
|
|
|
*/ |
38
|
|
|
private $tokenAuth; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var StatefulGuard|Guard |
42
|
|
|
*/ |
43
|
|
|
private $guard; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* AuthMutation constructor. |
47
|
|
|
* @param array $attributes |
48
|
|
|
* @param Guard $guard |
49
|
|
|
* @param TokenAuth $tokenAuth |
50
|
|
|
*/ |
51
|
|
|
public function __construct(array $attributes = [], Guard $guard, TokenAuth $tokenAuth) |
52
|
|
|
{ |
53
|
|
|
parent::__construct($attributes); |
54
|
|
|
|
55
|
|
|
$this->tokenAuth = $tokenAuth; |
56
|
|
|
$this->guard = $guard; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return ObjectType |
61
|
|
|
*/ |
62
|
|
|
public function type(): ObjectType |
63
|
|
|
{ |
64
|
|
|
return \GraphQL::type(AuthUserType::getName()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
public function queryArguments(): array |
71
|
|
|
{ |
72
|
|
|
return [ |
73
|
|
|
'email' => [ |
74
|
|
|
'name' => 'email', |
75
|
|
|
'type' => Type::nonNull(Type::string()), |
76
|
|
|
], |
77
|
|
|
'password' => [ |
78
|
|
|
'name' => 'password', |
79
|
|
|
'type' => Type::nonNull(Type::string()), |
80
|
|
|
], |
81
|
|
|
'remember' => [ |
82
|
|
|
'name' => 'remember', |
83
|
|
|
'type' => Type::boolean(), |
84
|
|
|
], |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
|
|
public function rules(): array |
92
|
|
|
{ |
93
|
|
|
return [ |
94
|
|
|
'email' => [ |
95
|
|
|
'email', |
96
|
|
|
'exists:users,email', |
97
|
|
|
], |
98
|
|
|
]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param $root |
103
|
|
|
* @param array $args |
104
|
|
|
* @return array |
105
|
|
|
* @throws AccessDeniedHttpException |
106
|
|
|
*/ |
107
|
|
|
public function resolve($root, $args) |
|
|
|
|
108
|
|
|
{ |
109
|
|
|
[$email, $password] = $this->getEmailAndPassword($args); |
|
|
|
|
110
|
|
|
|
111
|
|
|
$user = $this->tokenAuth->attemptFromEmailAndPassword($email, $password); |
112
|
|
|
|
113
|
|
|
if (! $user) { |
114
|
|
|
throw new AccessDeniedHttpException('User password are not correct'); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if (isset($args['remember']) && $this->guard instanceof StatefulGuard) { |
118
|
|
|
$this->guard->login($user, (bool)$args['remember']); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return AuthUserSerializer::serialize($user); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param array $args |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
private function getEmailAndPassword(array $args = []): array |
129
|
|
|
{ |
130
|
|
|
return [ |
131
|
|
|
Arr::get($args, 'email', ''), |
132
|
|
|
Arr::get($args, 'password', ''), |
133
|
|
|
]; |
134
|
|
|
} |
135
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.