2
2
3
3
namespace Iamnotstatic \LaravelAPIAuth \Http \Controllers \Auth ;
4
4
5
+ use App \User ;
6
+ use Illuminate \Http \Request ;
7
+ use Illuminate \Support \Carbon ;
5
8
use App \Http \Controllers \Controller ;
9
+ use Illuminate \Support \Facades \Validator ;
10
+ use Iamnotstatic \LaravelAPIAuth \Models \PasswordReset ;
11
+ use Iamnotstatic \LaravelAPIAuth \Notifications \PasswordResetSuccess ;
6
12
7
13
class ResetPasswordController extends Controller
8
14
{
@@ -17,5 +23,77 @@ class ResetPasswordController extends Controller
17
23
|
18
24
*/
19
25
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
+
20
98
21
99
}
0 commit comments