1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Cortex\Auth\Http\Controllers\Adminarea; |
6
|
|
|
|
7
|
|
|
use Cortex\Auth\Traits\TwoFactorAuthenticatesUsers; |
8
|
|
|
use Cortex\Foundation\Http\Controllers\AbstractController; |
9
|
|
|
use Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationRequest; |
10
|
|
|
use Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationSendRequest; |
11
|
|
|
use Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationVerifyRequest; |
12
|
|
|
use Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationProcessRequest; |
13
|
|
|
|
14
|
|
|
class PhoneVerificationController extends AbstractController |
15
|
|
|
{ |
16
|
|
|
use TwoFactorAuthenticatesUsers; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Show the phone verification form. |
20
|
|
|
* |
21
|
|
|
* @param \Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationRequest $request |
22
|
|
|
* |
23
|
|
|
* @return \Illuminate\View\View |
24
|
|
|
*/ |
25
|
|
|
public function request(PhoneVerificationRequest $request) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
return view('cortex/auth::adminarea.pages.verification-phone-request'); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Process the phone verification request form. |
32
|
|
|
* |
33
|
|
|
* @param \Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationSendRequest $request |
34
|
|
|
* |
35
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
36
|
|
|
*/ |
37
|
|
|
public function send(PhoneVerificationSendRequest $request) |
38
|
|
|
{ |
39
|
|
|
$user = $request->user($this->getGuard()) |
40
|
|
|
?? $request->attemptUser($this->getGuard()) |
|
|
|
|
41
|
|
|
?? app('cortex.auth.admin')->whereNotNull('phone')->where('phone', $request->input('phone'))->first(); |
|
|
|
|
42
|
|
|
|
43
|
|
|
$user->sendPhoneVerificationNotification($request->get('method'), true); |
44
|
|
|
|
45
|
|
|
return intend([ |
46
|
|
|
'url' => route('adminarea.verification.phone.verify', ['phone' => $user->phone]), |
47
|
|
|
'with' => ['success' => trans('cortex/auth::messages.verification.phone.sent')], |
48
|
|
|
]); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Show the phone verification form. |
53
|
|
|
* |
54
|
|
|
* @param \Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationVerifyRequest $request |
55
|
|
|
* |
56
|
|
|
* @return \Illuminate\View\View |
57
|
|
|
*/ |
58
|
|
|
public function verify(PhoneVerificationVerifyRequest $request) |
|
|
|
|
59
|
|
|
{ |
60
|
|
|
return view('cortex/auth::adminarea.pages.verification-phone-token'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Process the phone verification form. |
65
|
|
|
* |
66
|
|
|
* @param \Cortex\Auth\Http\Requests\Adminarea\PhoneVerificationProcessRequest $request |
67
|
|
|
* |
68
|
|
|
* @return \Illuminate\Http\JsonResponse|\Illuminate\Http\RedirectResponse |
69
|
|
|
*/ |
70
|
|
|
public function process(PhoneVerificationProcessRequest $request) |
71
|
|
|
{ |
72
|
|
|
// Guest trying to authenticate through TwoFactor |
73
|
|
|
if (($attemptUser = $request->attemptUser($this->getGuard())) && $this->attemptTwoFactor($attemptUser, $request->get('token'))) { |
|
|
|
|
74
|
|
|
auth()->guard($this->getGuard())->login($attemptUser, $request->session()->get('cortex.auth.twofactor.remember')); |
|
|
|
|
75
|
|
|
$request->session()->forget('cortex.auth.twofactor'); // @TODO: Do we need to forget session, or it's already gone after login? |
|
|
|
|
76
|
|
|
|
77
|
|
|
return intend([ |
78
|
|
|
'intended' => route('adminarea.home'), |
79
|
|
|
'with' => ['success' => trans('cortex/auth::messages.auth.login')], |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
// Logged in user OR A GUEST trying to verify phone |
84
|
|
|
if (($user = $request->user($this->getGuard()) ?? app('cortex.auth.admin')->whereNotNull('phone')->where('phone', $request->get('phone'))->first()) && $this->isValidTwoFactorPhone($user, $request->get('token'))) { |
|
|
|
|
85
|
|
|
// Profile update |
86
|
|
|
$user->fill([ |
87
|
|
|
'phone_verified' => true, |
88
|
|
|
'phone_verified_at' => now(), |
89
|
|
|
])->forceSave(); |
90
|
|
|
|
91
|
|
|
return intend([ |
92
|
|
|
'url' => route('adminarea.account.settings'), |
93
|
|
|
'with' => ['success' => trans('cortex/auth::messages.verification.phone.verified')], |
94
|
|
|
]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return intend([ |
98
|
|
|
'back' => true, |
99
|
|
|
'withErrors' => ['token' => trans('cortex/auth::messages.verification.twofactor.invalid_token')], |
100
|
|
|
]); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.