|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Backpack\CRUD\app\Http\Controllers\Auth; |
| 4 | + |
| 5 | +use Backpack\CRUD\app\Http\Requests\EmailVerificationRequest; |
| 6 | +use Backpack\CRUD\app\Library\Auth\UserFromCookie; |
| 7 | +use Exception; |
| 8 | +use Illuminate\Http\Request; |
| 9 | +use Illuminate\Routing\Controller; |
| 10 | +use Prologue\Alerts\Facades\Alert; |
| 11 | + |
| 12 | +class VerifyEmailController extends Controller |
| 13 | +{ |
| 14 | + public null|string $redirectTo = null; |
| 15 | + |
| 16 | + /** |
| 17 | + * Create a new controller instance. |
| 18 | + * |
| 19 | + * @return void |
| 20 | + */ |
| 21 | + public function __construct() |
| 22 | + { |
| 23 | + if (! app('router')->getMiddleware()['signed'] ?? null) { |
| 24 | + throw new Exception('Missing "signed" alias middleware in App/Http/Kernel.php. More info: https://backpackforlaravel.com/docs/6.x/base-how-to#enable-email-verification-in-backpack-routes'); |
| 25 | + } |
| 26 | + |
| 27 | + $this->middleware('signed')->only('verifyEmail'); |
| 28 | + $this->middleware('throttle:'.config('backpack.base.email_verification_throttle_access'))->only('resendVerificationEmail'); |
| 29 | + |
| 30 | + if (! backpack_users_have_email()) { |
| 31 | + abort(500, trans('backpack::base.no_email_column')); |
| 32 | + } |
| 33 | + // where to redirect after the email is verified |
| 34 | + $this->redirectTo = $this->redirectTo ?? backpack_url('dashboard'); |
| 35 | + } |
| 36 | + |
| 37 | + public function emailVerificationRequired(Request $request): \Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse |
| 38 | + { |
| 39 | + $this->getUserOrRedirect($request); |
| 40 | + |
| 41 | + return view(backpack_view('auth.verify-email')); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Verify the user's email address. |
| 46 | + * |
| 47 | + * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector |
| 48 | + */ |
| 49 | + public function verifyEmail(EmailVerificationRequest $request) |
| 50 | + { |
| 51 | + $this->getUserOrRedirect($request); |
| 52 | + |
| 53 | + $request->fulfill(); |
| 54 | + |
| 55 | + return redirect($this->redirectTo); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Resend the email verification notification. |
| 60 | + */ |
| 61 | + public function resendVerificationEmail(Request $request): \Illuminate\Http\RedirectResponse |
| 62 | + { |
| 63 | + $user = $this->getUserOrRedirect($request); |
| 64 | + |
| 65 | + if (is_a($user, \Illuminate\Http\RedirectResponse::class)) { |
| 66 | + return $user; |
| 67 | + } |
| 68 | + |
| 69 | + $user->sendEmailVerificationNotification(); |
| 70 | + Alert::success('Email verification link sent successfully.')->flash(); |
| 71 | + |
| 72 | + return back()->with('status', 'verification-link-sent'); |
| 73 | + } |
| 74 | + |
| 75 | + private function getUser(Request $request): ?\Illuminate\Contracts\Auth\MustVerifyEmail |
| 76 | + { |
| 77 | + return $request->user(backpack_guard_name()) ?? (new UserFromCookie())(); |
| 78 | + } |
| 79 | + |
| 80 | + private function getUserOrRedirect(Request $request): \Illuminate\Contracts\Auth\MustVerifyEmail|\Illuminate\Http\RedirectResponse |
| 81 | + { |
| 82 | + if ($user = $this->getUser($request)) { |
| 83 | + return $user; |
| 84 | + } |
| 85 | + |
| 86 | + return redirect()->route('backpack.auth.login'); |
| 87 | + } |
| 88 | +} |
0 commit comments