ForgotPasswordController::showResetRequestForm()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Oscer\Cms\Backend\Http\Controllers\Auth;
4
5
use Illuminate\Http\RedirectResponse;
6
use Illuminate\Support\Facades\Cache;
7
use Illuminate\Support\Facades\Mail;
8
use Illuminate\Support\Str;
9
use Illuminate\View\View;
10
use Oscer\Cms\Backend\Http\Requests\Auth\SendPasswordResetLinkRequest;
11
use Oscer\Cms\Core\Mails\ResetPasswordMail;
12
use Oscer\Cms\Core\Models\User;
13
use Throwable;
14
15
class ForgotPasswordController
16
{
17
    /**
18
     * Show the reset-password form.
19
     */
20
    public function showResetRequestForm(): View
21
    {
22
        return view('cms::auth.reset-request-form');
23
    }
24
25
    public function sendResetLinkEmail(SendPasswordResetLinkRequest $request): RedirectResponse
26
    {
27
        $user = User::query()->where('email', $request->input('email'))->first();
28
29
        $token = Cache::remember("password.reset.{$user->id}", now()->addMinutes(30), function () {
30
            return Str::random();
31
        });
32
33
        Mail::to($user->email)->send(new ResetPasswordMail(
34
            encrypt($user->id.'|'.$token)
35
        ));
36
37
        return redirect()->route('cms.password.forgot')->with('sent', true);
38
    }
39
40
    /**
41
     * Show the new password to the user.
42
     * @throws \Exception
43
     */
44
    public function showNewPassword(string $token)
45
    {
46
        try {
47
            $token = decrypt($token);
48
49
            [$userId, $token] = explode('|', $token);
0 ignored issues
show
Bug introduced by
The variable $userId does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
50
51
            /** @var \Oscer\Cms\Core\Models\User $user */
52
            $user = User::query()->findOrFail($userId);
53
        } catch (Throwable $e) {
54
            return redirect()->route('cms.password.forgot')->with('invalidResetToken', true);
55
        }
56
57
        if (cache("password.reset.{$userId}") != $token) {
58
            return redirect()->route('cms.password.forgot')->with('invalidResetToken', true);
59
        }
60
61
        cache()->forget("password.reset.{$userId}");
62
63
        $user->update(['password' => $password = Str::random()]);
64
65
        return view('cms::auth.reset-password', [
66
            'password' => $password,
67
        ]);
68
    }
69
}
70