1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Xetaravel\Http\Controllers\Auth; |
6
|
|
|
|
7
|
|
|
use Illuminate\Auth\Access\AuthorizationException; |
8
|
|
|
use Illuminate\Auth\Events\Verified; |
9
|
|
|
use Illuminate\Contracts\View\Factory; |
10
|
|
|
use Illuminate\Contracts\View\View; |
11
|
|
|
use Illuminate\Foundation\Application; |
12
|
|
|
use Illuminate\Foundation\Auth\VerifiesEmails; |
13
|
|
|
use Illuminate\Http\JsonResponse; |
14
|
|
|
use Illuminate\Http\RedirectResponse; |
15
|
|
|
use Illuminate\Http\Request; |
16
|
|
|
use Illuminate\Support\Facades\Auth; |
17
|
|
|
use Xetaravel\Http\Controllers\Controller; |
18
|
|
|
use Xetaravel\Models\User; |
19
|
|
|
|
20
|
|
|
class VerificationController extends Controller |
21
|
|
|
{ |
22
|
|
|
/* |
23
|
|
|
|-------------------------------------------------------------------------- |
24
|
|
|
| Email Verification Controller |
25
|
|
|
|-------------------------------------------------------------------------- |
26
|
|
|
| |
27
|
|
|
| This controller is responsible for handling email verification for any |
28
|
|
|
| user that recently registered with the application. Emails may also |
29
|
|
|
| be re-sent if the user didn't receive the original email message. |
30
|
|
|
| |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
use VerifiesEmails; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Where to redirect users after verification. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
protected string $redirectTo = 'auth/login'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a new controller instance. |
44
|
|
|
* |
45
|
|
|
* @return void |
46
|
|
|
*/ |
47
|
|
|
public function __construct() |
48
|
|
|
{ |
49
|
|
|
parent::__construct(); |
50
|
|
|
|
51
|
|
|
$this->middleware('signed')->only('verify'); |
52
|
|
|
$this->middleware('throttle:6,1')->only('verify', 'resend'); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Show the email verification notice. |
57
|
|
|
* |
58
|
|
|
* @param Request $request |
59
|
|
|
* @param string $hash The email of the user encoded to base64. |
60
|
|
|
* |
61
|
|
|
* @return Factory|View|Application|\Illuminate\View\View|object |
62
|
|
|
*/ |
63
|
|
|
public function show(Request $request, string $hash) |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
return view('Auth.verify', compact('hash')); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Mark the authenticated user's email address as verified. |
70
|
|
|
* |
71
|
|
|
* @param Request $request |
72
|
|
|
* |
73
|
|
|
* @return JsonResponse|RedirectResponse |
74
|
|
|
* |
75
|
|
|
* @throws AuthorizationException |
76
|
|
|
*/ |
77
|
|
|
public function verify(Request $request) |
78
|
|
|
{ |
79
|
|
|
$user = User::find($request->route('id')); |
80
|
|
|
|
81
|
|
|
if (!hash_equals((string) $request->route('id'), (string) $user->getKey())) { |
82
|
|
|
throw new AuthorizationException(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if (!hash_equals((string) $request->route('hash'), base64_encode($user->getEmailForVerification()))) { |
86
|
|
|
throw new AuthorizationException(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$user = User::find($request->route('id')); |
90
|
|
|
|
91
|
|
|
if ($user->hasVerifiedEmail()) { |
92
|
|
|
return $request->wantsJson() |
93
|
|
|
? new JsonResponse([], 204) |
94
|
|
|
: redirect($this->redirectPath()); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if ($user->markEmailAsVerified()) { |
98
|
|
|
event(new Verified($user)); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($response = $this->verified($request)) { |
|
|
|
|
102
|
|
|
return $response; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
Auth::login($user, true); |
106
|
|
|
|
107
|
|
|
return $request->wantsJson() |
108
|
|
|
? new JsonResponse([], 204) |
109
|
|
|
: redirect($this->redirectPath()) |
110
|
|
|
->with('verified', true) |
111
|
|
|
->success("Your Email has been verified!"); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Resend the email verification notification. |
116
|
|
|
* |
117
|
|
|
* @param Request $request |
118
|
|
|
* |
119
|
|
|
* @return JsonResponse|RedirectResponse |
120
|
|
|
*/ |
121
|
|
|
public function resend(Request $request) |
122
|
|
|
{ |
123
|
|
|
$email = base64_decode($request->input('hash')); |
124
|
|
|
$user = User::where('email', $email)->first(); |
125
|
|
|
|
126
|
|
|
if (!$user || $user->hasVerifiedEmail()) { |
127
|
|
|
return $request->wantsJson() |
128
|
|
|
? new JsonResponse([], 204) |
129
|
|
|
: redirect($this->redirectPath()); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$user->sendEmailVerificationNotification(); |
133
|
|
|
|
134
|
|
|
return $request->wantsJson() |
135
|
|
|
? new JsonResponse([], 202) |
136
|
|
|
: back()->with('resent', true); |
|
|
|
|
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.