Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2ba8f91

Browse files
password reset func added
1 parent c597f39 commit 2ba8f91

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

‎src/Http/Controllers/Auth/ResetPasswordController.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
namespace Iamnotstatic\LaravelAPIAuth\Http\Controllers\Auth;
44

5+
use App\User;
6+
use Illuminate\Http\Request;
7+
use Illuminate\Support\Carbon;
58
use App\Http\Controllers\Controller;
9+
use Illuminate\Support\Facades\Validator;
10+
use Iamnotstatic\LaravelAPIAuth\Models\PasswordReset;
11+
use Iamnotstatic\LaravelAPIAuth\Notifications\PasswordResetSuccess;
612

713
class ResetPasswordController extends Controller
814
{
@@ -17,5 +23,77 @@ class ResetPasswordController extends Controller
1723
|
1824
*/
1925

26+
/**
27+
* Find token password reset
28+
*
29+
* @param [string] $token
30+
* @return [string] message
31+
* @return [json] passwordReset object
32+
*/
33+
public function find($token)
34+
{
35+
$passwordReset = PasswordReset::where('token', $token)->first();
36+
37+
if (!$passwordReset)
38+
return response()->json([ 'error' => 'This password reset token is invalid.'], 404);
39+
40+
if (Carbon::parse($passwordReset->updated_at)->addMinutes(720)->isPast()) {
41+
$passwordReset->delete();
42+
return response()->json([ 'error' => 'This password reset token is invalid.'], 404);
43+
}
44+
45+
return response()->json($passwordReset);
46+
47+
}
48+
49+
50+
/**
51+
* Reset password
52+
*
53+
* @param [string] email
54+
* @param [string] password
55+
* @param [string] password_confirmation
56+
* @param [string] token
57+
* @return [string] message
58+
* @return [json] user object
59+
*/
60+
public function reset(Request $request)
61+
{
62+
$validate = Validator::make($request->all(), [
63+
'email' => 'required|string|email',
64+
'password' => 'required|string|min:8|confirmed',
65+
'token' => 'required|string'
66+
]);
67+
68+
if ($validate->fails()) {
69+
return response()->json(['error' => $validate->errors()], 400);
70+
}
71+
72+
$passwordReset = PasswordReset::where([
73+
['token', $request->token],
74+
['email', $request->email]
75+
])->first();
76+
77+
if (!$passwordReset)
78+
return response()->json([ "error" => "This password reset token is invalid."], 404);
79+
80+
$user = User::where('email', $passwordReset->email)->first();
81+
82+
if (!$user)
83+
return response()->json(["error" => "We can't find a user with that e-mail address."], 404);
84+
85+
$user->password = bcrypt($request->password);
86+
$user->save();
87+
$passwordReset->delete();
88+
89+
try {
90+
$user->notify(new PasswordResetSuccess($passwordReset));
91+
} catch (\Throwable $th) {
92+
//throw $th;
93+
}
94+
95+
return response()->json(['success' => 'Password was reset successfully you can now login']);
96+
}
97+
2098

2199
}

‎src/routes/api.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
Route::post('login', 'Auth\LoginController@login')->name('login');
2323

2424
Route::post('password/forgotten', 'Auth\ForgotPasswordController@forgotten');
25-
Route::get('password/find/{token}', 'Auth\PasswordResetController@find');
26-
Route::post('password/reset', 'Api\Password\PasswordResetController@reset');
25+
Route::get('password/find/{token}', 'Auth\ResetPasswordController@find');
26+
Route::post('password/reset', 'Auth\ResetPasswordController@reset');
2727

2828
});
2929

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /