1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\ResetPasswordRequest; |
|
|
|
|
6
|
|
|
use App\Models\User; |
7
|
|
|
use Auth; |
8
|
|
|
use Carbon\Carbon; |
9
|
|
|
use DB; |
10
|
|
|
use Hash; |
11
|
|
|
use Illuminate\Http\Request; |
12
|
|
|
use Illuminate\Support\Facades\Password; |
13
|
|
|
use Illuminate\Support\Str; |
14
|
|
|
use Spatie\Multitenancy\Models\Concerns\UsesLandlordConnection; |
15
|
|
|
|
16
|
|
|
class ForgotPasswordController extends Controller |
17
|
|
|
{ |
18
|
|
|
use UsesLandlordConnection; |
19
|
|
|
|
20
|
|
|
public function forgot(Request $request) |
21
|
|
|
{ |
22
|
|
|
$credentials = request()->validate(['email' => 'required|email']); |
|
|
|
|
23
|
|
|
$user = User::where('email', $request->email)->first(); |
24
|
|
|
if (! $user) { |
25
|
|
|
return response()->json(['error' => '404', 'error_msg' => 'Email not exits in record']); |
26
|
|
|
} |
27
|
|
|
$token = $this->generateToken(); |
28
|
|
|
DB::connection($this->getConnectionName())->table('password_resets')->insert([ |
29
|
|
|
'email' => $request->email, |
30
|
|
|
'token' => $token, //change 60 to any length you want |
31
|
|
|
'created_at' => Carbon::now(), |
32
|
|
|
]); |
33
|
|
|
$user->sendPasswordResetNotification($token); |
34
|
|
|
|
35
|
|
|
return response()->json(['msg' => 'Reset password link sent on your email id.']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private function generateToken() |
39
|
|
|
{ |
40
|
|
|
// This is set in the .env file |
41
|
|
|
$key = config('app.key'); |
42
|
|
|
|
43
|
|
|
// Illuminate\Support\Str; |
44
|
|
|
if (Str::startsWith($key, 'base64:')) { |
45
|
|
|
$key = base64_decode(substr($key, 7)); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return hash_hmac('sha256', Str::random(40), $key); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function reset(Request $request) |
52
|
|
|
{ |
53
|
|
|
$credentials = request()->validate([ |
|
|
|
|
54
|
|
|
'email' => 'required|email', |
55
|
|
|
'token' => 'required|string', |
56
|
|
|
'password' => 'required|string|confirmed', |
57
|
|
|
]); |
58
|
|
|
$tokenData = DB::connection($this->getConnectionName())->table('password_resets') |
59
|
|
|
->where('token', $request->token)->where('email', $request->email)->first(); |
60
|
|
|
if ($tokenData) { |
61
|
|
|
$user = User::where('email', $request->email)->first(); |
62
|
|
|
if ($user) { |
63
|
|
|
$user->password = bcrypt($request->password); |
64
|
|
|
$user->save(); //or $user->save(); |
65
|
|
|
DB::connection($this->getConnectionName())->table('password_resets')->where('email', $user->email)->delete(); |
66
|
|
|
|
67
|
|
|
return response()->json(['msg' => 'Password has been successfully changed.']); |
68
|
|
|
} else { |
69
|
|
|
return response()->json(['error_msg' => 'User Not available.']); |
70
|
|
|
} |
71
|
|
|
} else { |
72
|
|
|
return response()->json(['error_msg' => 'Token not match for this email.']); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths