|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cortex\Auth\Http\Controllers\Tenantarea; |
|
6
|
|
|
|
|
7
|
|
|
use Cortex\Auth\Traits\TwoFactorAuthenticatesUsers; |
|
8
|
|
|
use Cortex\Foundation\Http\Controllers\AbstractController; |
|
9
|
|
|
use Cortex\Auth\Http\Requests\Tenantarea\PhoneVerificationRequest; |
|
10
|
|
|
use Cortex\Auth\Http\Requests\Tenantarea\PhoneVerificationSendRequest; |
|
11
|
|
|
use Cortex\Auth\Http\Requests\Tenantarea\PhoneVerificationVerifyRequest; |
|
12
|
|
|
use Cortex\Auth\Http\Requests\Tenantarea\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\Tenantarea\PhoneVerificationRequest $request |
|
22
|
|
|
* |
|
23
|
|
|
* @return \Illuminate\View\View |
|
24
|
|
|
*/ |
|
25
|
|
|
public function request(PhoneVerificationRequest $request) |
|
|
|
|
|
|
26
|
|
|
{ |
|
27
|
|
|
return view('cortex/auth::tenantarea.pages.verification-phone-request'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Process the phone verification request form. |
|
32
|
|
|
* |
|
33
|
|
|
* @param \Cortex\Auth\Http\Requests\Tenantarea\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.member')->whereNotNull('phone')->where('phone', $request->input('phone'))->first(); |
|
42
|
|
|
|
|
43
|
|
|
$user->sendPhoneVerificationNotification($request->get('method'), true); |
|
44
|
|
|
|
|
45
|
|
|
return intend([ |
|
46
|
|
|
'url' => route('tenantarea.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\Tenantarea\PhoneVerificationVerifyRequest $request |
|
55
|
|
|
* |
|
56
|
|
|
* @return \Illuminate\View\View |
|
57
|
|
|
*/ |
|
58
|
|
|
public function verify(PhoneVerificationVerifyRequest $request) |
|
|
|
|
|
|
59
|
|
|
{ |
|
60
|
|
|
return view('cortex/auth::tenantarea.pages.verification-phone-token'); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Process the phone verification form. |
|
65
|
|
|
* |
|
66
|
|
|
* @param \Cortex\Auth\Http\Requests\Tenantarea\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('tenantarea.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.member')->whereNotNull('phone')->where('phone', $request->get('phone'))->first()) && $this->isValidTwoFactorPhone($user, $request->get('token'))) { |
|
85
|
|
|
// Profile update |
|
86
|
|
|
$user->fill([ |
|
87
|
|
|
'phone_verified_at' => now(), |
|
88
|
|
|
])->forceSave(); |
|
89
|
|
|
|
|
90
|
|
|
return intend([ |
|
91
|
|
|
'url' => route('tenantarea.account.settings'), |
|
92
|
|
|
'with' => ['success' => trans('cortex/auth::messages.verification.phone.verified')], |
|
93
|
|
|
]); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return intend([ |
|
97
|
|
|
'back' => true, |
|
98
|
|
|
'withErrors' => ['token' => trans('cortex/auth::messages.verification.twofactor.invalid_token')], |
|
99
|
|
|
]); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.