1
|
|
|
<?php namespace App\Http\Controllers\Auth; |
2
|
|
|
|
3
|
|
|
use App\Events\Users\RequestedResetPasswordLink; |
4
|
|
|
use App\Events\Users\ResetPassword; |
5
|
|
|
use App\Exceptions\Common\ValidationException; |
6
|
|
|
use App\Exceptions\Users\TokenNotValidException; |
7
|
|
|
use App\Http\Controllers\Controller; |
8
|
|
|
use App\Models\User; |
9
|
|
|
use App\Notifications\ResetPasswordNotification; |
10
|
|
|
use Illuminate\Contracts\Auth\PasswordBroker; |
11
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
12
|
|
|
use Illuminate\Foundation\Auth\RedirectsUsers; |
13
|
|
|
use Illuminate\Http\Request; |
14
|
|
|
|
15
|
|
|
class PasswordController extends Controller |
16
|
|
|
{ |
17
|
|
|
use RedirectsUsers; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Create a new password controller instance. |
21
|
|
|
*/ |
22
|
6 |
|
public function __construct() |
23
|
|
|
{ |
24
|
6 |
|
$this->middleware('guest'); |
25
|
6 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Display the form to request a password reset link. |
29
|
|
|
* |
30
|
|
|
* @return \Illuminate\View\View|\Illuminate\Contracts\View\Factory |
31
|
|
|
*/ |
32
|
2 |
|
public function requestPasswordResetLink() |
33
|
|
|
{ |
34
|
2 |
|
return view('password/email'); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Send a password reset link to the given email's owner, via email. |
39
|
|
|
* |
40
|
|
|
* @param \Illuminate\Http\Request $request |
41
|
|
|
* |
42
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
43
|
|
|
* @throws \App\Exceptions\Common\ValidationException |
44
|
|
|
*/ |
45
|
2 |
|
public function sendPasswordResetLink(Request $request) |
46
|
|
|
{ |
47
|
2 |
|
$validator = app('validator')->make($request->all(), [ |
48
|
|
|
'email' => 'required|email|max:255' |
49
|
2 |
|
]); |
50
|
2 |
|
if ($validator->fails()) { |
51
|
1 |
|
throw new ValidationException($validator); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
$user = User::whereEmail($request->only('email'))->first(); |
|
|
|
|
55
|
2 |
|
if (is_null($user)) { |
56
|
1 |
|
throw new ModelNotFoundException(trans('passwords.user')); |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
$user->notify(new ResetPasswordNotification(app('auth.password.broker')->createToken($user))); |
60
|
|
|
|
61
|
1 |
|
event(new RequestedResetPasswordLink($user)); |
62
|
|
|
|
63
|
1 |
|
if ($request->expectsJson()) { |
64
|
1 |
|
return response()->json(['message' => trans('passwords.sent')]); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return redirect()->back()->with('message', trans('passwords.sent')); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Display the password reset view for the given token. |
72
|
|
|
* |
73
|
|
|
* @param \Illuminate\Http\Request $request |
74
|
|
|
* @param string $token |
75
|
|
|
* |
76
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\View\View|\Illuminate\Contracts\View\Factory |
77
|
|
|
*/ |
78
|
|
|
public function showPasswordResetForm(Request $request, $token = null) |
79
|
|
|
{ |
80
|
|
|
if (is_null($token)) { |
81
|
|
|
if ($request->expectsJson()) { |
82
|
|
|
throw new TokenNotValidException(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return view('password/reset')->withErrors(['token' => trans(PasswordBroker::INVALID_TOKEN)]); |
|
|
|
|
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
if ($request->expectsJson()) { |
89
|
|
|
return response()->json(['token' => $token]); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return view('password/reset')->with('token', $token); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Reset the password through password-reset-token and email provided. |
97
|
|
|
* |
98
|
|
|
* @param \Illuminate\Http\Request $request |
99
|
|
|
* |
100
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
101
|
|
|
* @throws \App\Exceptions\Common\ValidationException |
102
|
|
|
*/ |
103
|
2 |
|
public function resetPassword(Request $request) |
104
|
|
|
{ |
105
|
2 |
|
$validator = app('validator')->make($request->all(), [ |
106
|
2 |
|
'token' => 'required|string', |
107
|
2 |
|
'email' => 'required|email|max:255', |
108
|
2 |
|
'password' => 'required|confirmed|min:' . app('config')->get('auth.passwords.users.min_length') |
109
|
|
|
]); |
110
|
2 |
|
if ($validator->fails()) { |
111
|
1 |
|
throw new ValidationException($validator); |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
$credentials = $request->only('email', 'password', 'password_confirmation', 'token'); |
115
|
|
|
|
116
|
2 |
|
$passwordBroker = app('auth.password.broker'); |
117
|
2 |
|
$response = $passwordBroker->reset( |
118
|
2 |
|
$credentials, function (User $user, $password) { |
119
|
1 |
|
$user->password = app('hash')->make($password); |
120
|
1 |
|
$user->save(); |
121
|
1 |
|
app('auth.driver')->login($user); |
122
|
2 |
|
}); |
123
|
|
|
|
124
|
|
|
switch ($response) { |
125
|
2 |
|
case $passwordBroker::INVALID_USER: |
126
|
1 |
|
throw new ModelNotFoundException(trans($response)); |
127
|
|
|
break; |
|
|
|
|
128
|
2 |
|
case $passwordBroker::INVALID_TOKEN: |
129
|
1 |
|
throw new TokenNotValidException(trans($response)); |
130
|
|
|
break; |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
event(new ResetPassword(app('auth.driver')->user())); |
134
|
|
|
|
135
|
1 |
|
if ($request->expectsJson()) { |
136
|
1 |
|
return response()->json(['message' => trans('passwords.reset')]); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return redirect($this->redirectPath())->with('message', trans('passwords.reset')); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: