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\Mutations; |
10
|
|
|
|
11
|
|
|
use App\Models\User; |
12
|
|
|
use App\Services\TokenAuth; |
13
|
|
|
use Illuminate\Support\Arr; |
14
|
|
|
use App\GraphQL\Types\UserType; |
15
|
|
|
use GraphQL\Type\Definition\Type; |
16
|
|
|
use GraphQL\Type\Definition\ObjectType; |
17
|
|
|
use App\GraphQL\Serializers\UserSerializer; |
18
|
|
|
use Illuminate\Contracts\Auth\Authenticatable; |
19
|
|
|
use Illuminate\Contracts\Validation\Validator; |
20
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class AuthMutation |
24
|
|
|
* @package App\GraphQL\Mutations |
25
|
|
|
*/ |
26
|
|
|
class AuthMutation extends AbstractMutation |
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
|
|
|
* AuthorisationQuery constructor. |
42
|
|
|
* @param array $attributes |
43
|
|
|
* @param TokenAuth $tokenAuth |
44
|
|
|
*/ |
45
|
|
|
public function __construct(array $attributes = [], TokenAuth $tokenAuth) |
46
|
|
|
{ |
47
|
|
|
parent::__construct($attributes); |
48
|
|
|
|
49
|
|
|
$this->tokenAuth = $tokenAuth; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return ObjectType |
54
|
|
|
*/ |
55
|
|
|
public function type(): ObjectType |
56
|
|
|
{ |
57
|
|
|
return \GraphQL::type(UserType::getName()); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @return array |
62
|
|
|
*/ |
63
|
|
|
public function args(): array |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
|
|
'email' => [ |
67
|
|
|
'name' => 'email', |
68
|
|
|
'type' => Type::nonNull(Type::string()), |
69
|
|
|
], |
70
|
|
|
'password' => [ |
71
|
|
|
'name' => 'password', |
72
|
|
|
'type' => Type::nonNull(Type::string()), |
73
|
|
|
], |
74
|
|
|
'remember' => [ |
75
|
|
|
'name' => 'remember', |
76
|
|
|
'type' => Type::boolean(), |
77
|
|
|
], |
78
|
|
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
public function rules(): array |
85
|
|
|
{ |
86
|
|
|
return [ |
87
|
|
|
'email' => [ |
88
|
|
|
'email', |
89
|
|
|
'exists:users,email', |
90
|
|
|
], |
91
|
|
|
]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param $root |
96
|
|
|
* @param array $args |
97
|
|
|
* @return array |
98
|
|
|
* @throws AccessDeniedHttpException |
99
|
|
|
*/ |
100
|
|
|
public function resolve($root, $args) |
|
|
|
|
101
|
|
|
{ |
102
|
|
|
[$email, $password] = $this->getEmailAndPassword($args); |
|
|
|
|
103
|
|
|
|
104
|
|
|
$user = $this->tokenAuth->attemptFromEmailAndPassword($email, $password); |
105
|
|
|
|
106
|
|
|
if (! $user) { |
107
|
|
|
throw new AccessDeniedHttpException('User password are not correct'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$user->token = $this->tokenAuth->fromUser($user); |
|
|
|
|
111
|
|
|
|
112
|
|
|
return UserSerializer::serialize($user); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param array $args |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
private function getEmailAndPassword(array $args = []): array |
120
|
|
|
{ |
121
|
|
|
return [ |
122
|
|
|
Arr::get($args, 'email', ''), |
123
|
|
|
Arr::get($args, 'password', ''), |
124
|
|
|
]; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.