1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Http\Controllers\Api\Registration; |
6
|
|
|
|
7
|
|
|
use App\Auth\Dispensary\Exceptions\TokenExpiredException; |
8
|
|
|
use App\Auth\RegistrationDispensary; |
9
|
|
|
use App\Contracts\Http\Responses\ResponseFactory; |
10
|
|
|
use App\Http\Requests\Api\Registration\VerifyRequest; |
11
|
|
|
use App\Models\User; |
12
|
|
|
use Illuminate\Contracts\Hashing\Hasher; |
13
|
|
|
use Illuminate\Http\Response; |
14
|
|
|
|
15
|
|
|
final class Verify |
16
|
|
|
{ |
17
|
|
|
private ResponseFactory $responseFactory; |
18
|
|
|
|
19
|
|
|
private Hasher $hasher; |
20
|
|
|
|
21
|
|
|
private RegistrationDispensary $dispensary; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
ResponseFactory $responseFactory, |
25
|
|
|
RegistrationDispensary $dispensary, |
26
|
|
|
Hasher $hasher |
27
|
|
|
) { |
28
|
|
|
$this->responseFactory = $responseFactory; |
29
|
|
|
$this->hasher = $hasher; |
30
|
|
|
$this->dispensary = $dispensary; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param \App\Http\Requests\Api\Registration\VerifyRequest $request |
35
|
|
|
* @return \Illuminate\Http\Response |
36
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
37
|
|
|
*/ |
38
|
|
|
public function __invoke(VerifyRequest $request): Response |
39
|
|
|
{ |
40
|
|
|
$email = $request->input('email'); |
41
|
|
|
$token = $request->input('token'); |
42
|
|
|
$password = $request->input('password'); |
43
|
|
|
|
44
|
|
|
/** @var User $user */ |
45
|
|
|
$user = User::query()->where('email', $email)->first(); |
46
|
|
|
|
47
|
|
|
if (! $user instanceof User) { |
|
|
|
|
48
|
|
|
return $this->responseFactory->noContent(Response::HTTP_BAD_REQUEST); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
if ($this->dispensary->verify($user, $token)) { |
53
|
|
|
$user->update([ |
54
|
|
|
'password' => $this->hasher->make($password), |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
return $this->responseFactory->noContent(Response::HTTP_OK); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->responseFactory->noContent(Response::HTTP_BAD_REQUEST); |
61
|
|
|
} catch (TokenExpiredException $e) { |
62
|
|
|
return $this->responseFactory->noContent(Response::HTTP_BAD_REQUEST); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|