ForgotPasswordController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 34
dl 0
loc 57
rs 10
c 2
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A forgot() 0 16 2
A generateToken() 0 11 2
A reset() 0 22 3
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Http\Requests\ResetPasswordRequest;
0 ignored issues
show
Bug introduced by
The type App\Http\Requests\ResetPasswordRequest was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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']);
0 ignored issues
show
Unused Code introduced by
The assignment to $credentials is dead and can be removed.
Loading history...
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([
0 ignored issues
show
Unused Code introduced by
The assignment to $credentials is dead and can be removed.
Loading history...
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