1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Api\v1\Auth; |
4
|
|
|
|
5
|
|
|
use App\Helpers\Interfaces\ResponseCodesInterface; |
6
|
|
|
use App\Helpers\JsonApiResponseHelper; |
7
|
|
|
use App\Http\Controllers\Controller; |
8
|
|
|
use App\Services\PasswordService; |
9
|
|
|
use Illuminate\Foundation\Auth\SendsPasswordResetEmails; |
10
|
|
|
use Illuminate\Http\JsonResponse; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
use Validator; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class ForgotPasswordController |
16
|
|
|
* @package App\Http\Controllers\Api\v1\Auth |
17
|
|
|
*/ |
18
|
|
|
class ForgotPasswordController extends Controller implements ResponseCodesInterface |
19
|
|
|
{ |
20
|
|
|
/* |
21
|
|
|
|-------------------------------------------------------------------------- |
22
|
|
|
| Password Reset Controller |
23
|
|
|
|-------------------------------------------------------------------------- |
24
|
|
|
| |
25
|
|
|
| This controller is responsible for handling password reset emails and |
26
|
|
|
| includes a trait which assists in sending these notifications from |
27
|
|
|
| your application to your users. Feel free to explore this trait. |
28
|
|
|
| |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
use SendsPasswordResetEmails, JsonApiResponseHelper; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ForgotPasswordController constructor. |
35
|
|
|
*/ |
36
|
|
|
public function __construct() |
37
|
|
|
{ |
38
|
|
|
$this->middleware('guest'); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @SWG\Post(path="/password/forgot", |
43
|
|
|
* tags={"User actions"}, |
44
|
|
|
* summary="Perform user password forgot", |
45
|
|
|
* description="forgot user password", |
46
|
|
|
* produces={"application/json"}, |
47
|
|
|
* consumes={"application/json"}, |
48
|
|
|
* @SWG\Parameter( |
49
|
|
|
* in="body", |
50
|
|
|
* name="forgot password object", |
51
|
|
|
* description="JSON Object which forgot user password", |
52
|
|
|
* required=true, |
53
|
|
|
* @SWG\Schema( |
54
|
|
|
* type="object", |
55
|
|
|
* @SWG\Property(property="email", type="string", example="[email protected]") |
56
|
|
|
* ) |
57
|
|
|
* ), |
58
|
|
|
* @SWG\Response(response="200", description="Return message") |
59
|
|
|
* ) |
60
|
|
|
* |
61
|
|
|
* @param PasswordService $passwordService |
62
|
|
|
* @param Request $request |
63
|
|
|
* |
64
|
|
|
* @return JsonResponse |
65
|
|
|
*/ |
66
|
|
|
public function sendResetLinkEmail(PasswordService $passwordService, Request $request) |
67
|
|
|
{ |
68
|
|
|
$errors = $this->validateRequest($request->all())->errors(); |
69
|
|
|
|
70
|
|
|
if (!empty($errors->all())) { |
71
|
|
|
return $this->sendFailedResponse($errors->toArray(), self::HTTP_CODE_UNPROCESSABLE_ENTITY); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$reset = $passwordService->sendResetPasswordEmail($request->get('email')); |
75
|
|
|
|
76
|
|
|
if ($reset) { |
77
|
|
|
return $this->sendSuccessResponse(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$errors = [ |
81
|
|
|
[ |
82
|
|
|
'status' => 404, |
83
|
|
|
'title' => 'Email error', |
84
|
|
|
'detail' => 'Entered email is not found. Please make sure you are using existing email address and try again.' |
85
|
|
|
] |
86
|
|
|
|
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
return $this->sendFailedResponse($errors, self::HTTP_CODE_NOT_FOUND); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param array $data |
94
|
|
|
* @return \Illuminate\Validation\Validator |
95
|
|
|
*/ |
96
|
|
|
protected function validateRequest(array $data) |
97
|
|
|
{ |
98
|
|
|
$messages = [ |
99
|
|
|
'email.required' => 'Email field can\'t be blank' |
100
|
|
|
]; |
101
|
|
|
|
102
|
|
|
return Validator::make($data, [ |
103
|
|
|
'email' => 'required|email' |
104
|
|
|
], $messages); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|