/
VerificationController.php
90 lines (76 loc) · 2.69 KB
/
VerificationController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use App\Models\Recipient;
use App\Models\User;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Auth\Events\Verified;
use Illuminate\Foundation\Auth\VerifiesEmails;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
class VerificationController extends Controller
{
/*
|--------------------------------------------------------------------------
| Email Verification Controller
|--------------------------------------------------------------------------
|
| This controller is responsible for handling email verification for any
| user that recently registered with the application. Emails may also
| be re-sent if the user didn't receive the original email message.
|
*/
use VerifiesEmails;
/**
* Where to redirect users after verification.
*
* @var string
*/
protected $redirectTo = '/';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth')->except('verify');
$this->middleware('signed')->only('verify');
$this->middleware('throttle:1,1')->only('resend');
$this->middleware('throttle:6,1')->only('verify');
}
/**
* Mark the authenticated user's email address as verified.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function verify(Request $request)
{
$verifiable = User::find($request->route('id')) ?? Recipient::find($request->route('id'));
if (is_null($verifiable)) {
throw new AuthorizationException;
}
if (! hash_equals((string) $request->route('id'), (string) $verifiable->getKey())) {
throw new AuthorizationException;
}
if (! Hash::check($verifiable->getEmailForVerification(), (string) base64_decode($request->route('hash')))) {
throw new AuthorizationException;
}
if ($verifiable->hasVerifiedEmail()) {
return redirect($this->redirectPath());
}
if ($verifiable->markEmailAsVerified() && $verifiable instanceof User) {
event(new Verified($verifiable));
}
if ($request->user() !== null) {
$redirect = $verifiable instanceof User ? $this->redirectPath() : route('recipients.index');
} else {
$redirect = 'login';
}
return redirect($redirect)
->with('verified', true)
->with(['status' => 'Email Address Verified Successfully']);
}
}