Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add email confirmation #5301

Merged
merged 32 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a00bcef
add email verification
pxpm Aug 28, 2023
977276b
Apply fixes from StyleCI
StyleCIBot Aug 28, 2023
0c65162
add email confirmation
pxpm Aug 29, 2023
2f86b6e
Apply fixes from StyleCI
StyleCIBot Aug 29, 2023
5e83e25
Update src/app/Http/Controllers/Auth/VerifyEmailController.php
pxpm Aug 29, 2023
84969da
Update src/config/backpack/base.php
pxpm Aug 29, 2023
99de011
Update src/resources/lang/en/base.php
pxpm Aug 29, 2023
f75314c
Update src/resources/lang/en/base.php
pxpm Aug 29, 2023
14ee78e
dont alias signed and verified middlewares
pxpm Aug 29, 2023
e4f74e0
Merge branch 'add-email-confirmation' of https://github.com/Laravel-B…
pxpm Aug 29, 2023
cd092b7
small fixes
pxpm Aug 29, 2023
f60f722
Merge branch 'main' into add-email-confirmation
pxpm Aug 30, 2023
fa36181
add verification to middleware aliases
pxpm Aug 30, 2023
52c1341
Apply fixes from StyleCI
StyleCIBot Aug 30, 2023
15fe548
fixes
pxpm Aug 30, 2023
669823c
Merge branch 'add-email-confirmation' of https://github.com/Laravel-B…
pxpm Aug 30, 2023
fbd760a
Apply fixes from StyleCI
StyleCIBot Aug 30, 2023
1c36a61
Update src/app/Http/Controllers/Auth/VerifyEmailController.php
pxpm Aug 30, 2023
6e21330
add invokable
pxpm Aug 30, 2023
7ea81fa
move to function
pxpm Aug 30, 2023
25bc08c
Apply fixes from StyleCI
StyleCIBot Aug 30, 2023
d2d8958
return early
pxpm Aug 30, 2023
d78729c
Apply fixes from StyleCI
StyleCIBot Aug 30, 2023
3dc83ee
Apply suggestions from code review
pxpm Aug 31, 2023
252bfa6
Apply fixes from StyleCI
StyleCIBot Aug 31, 2023
431b24e
Merge branch 'main' into add-email-confirmation
pxpm Aug 31, 2023
0910fc2
Apply suggestions from code review
pxpm Aug 31, 2023
489c327
Merge branch 'add-email-confirmation' of https://github.com/Laravel-B…
pxpm Aug 31, 2023
2e912ac
fixes
pxpm Aug 31, 2023
d2a0f59
fix
pxpm Aug 31, 2023
b0c2c50
Apply fixes from StyleCI
StyleCIBot Aug 31, 2023
cbb430a
Update src/config/backpack/base.php
tabacitu Aug 31, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/BackpackServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ public function registerMiddlewareGroup(Router $router)
$router->aliasMiddleware('backpack.throttle.password.recovery', ThrottlePasswordRecovery::class);
}

// register the email verification middleware, if the developer the config is enabled
if (config('backpack.base.setup_email_verification_routes', false)) {
// register the email verification middleware, if the developer enabled it in the config.
if (config('backpack.base.setup_email_verification_routes', false) && config('backpack.base.add_verified_to_backpack_middleware', true)) {
pxpm marked this conversation as resolved.
Show resolved Hide resolved
$router->pushMiddlewareToGroup($middleware_key, EnsureEmailVerification::class);
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Validator;

class RegisterController extends Controller
Expand Down Expand Up @@ -113,12 +114,13 @@ public function register(Request $request)
$user = $this->create($request->all());

event(new Registered($user));
$this->guard()->login($user);

if (config('backpack.base.setup_email_verification_routes')) {
return view(backpack_view('auth.verify_email'));
Cookie::queue('backpack_email_verification', $user->{config('backpack.base.email_column')}, 30);
return redirect(route('verification.notice'));
}

$this->guard()->login($user);

return redirect($this->redirectPath());
}

Expand Down
45 changes: 36 additions & 9 deletions src/app/Http/Controllers/Auth/VerifyEmailController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Prologue\Alerts\Facades\Alert;
use Throwable;
use Exception;
use Illuminate\Support\Facades\Cookie;

class VerifyEmailController extends Controller
{
Expand All @@ -20,15 +21,11 @@ class VerifyEmailController extends Controller
*/
public function __construct()
{
$this->middleware(backpack_middleware());

try {
$signedMiddleware = new (app('router')->getMiddleware()['signed'])();
} catch(Throwable) {
if(! app('router')->getMiddleware()['signed'] ?? null) {
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');
}

$this->middleware($signedMiddleware)->only('verifyEmail');
$this->middleware('signed')->only('verifyEmail');
tabacitu marked this conversation as resolved.
Show resolved Hide resolved
$this->middleware('throttle:'.config('backpack.base.email_verification_throttle_access'))->only('resendVerificationEmail');

if (! backpack_users_have_email()) {
Expand All @@ -38,8 +35,14 @@ public function __construct()
$this->redirectTo = $this->redirectTo !== null ? $this->redirectTo : backpack_url('dashboard');
pxpm marked this conversation as resolved.
Show resolved Hide resolved
}

public function emailVerificationRequired(): \Illuminate\Contracts\View\View
public function emailVerificationRequired(): \Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
{
$user = $this->getUserFromCookie();

if (! $user) {
return redirect()->route('backpack.auth.login');
}

return view(backpack_view('auth.verify-email'));
}

Expand All @@ -50,6 +53,12 @@ public function emailVerificationRequired(): \Illuminate\Contracts\View\View
*/
public function verifyEmail(EmailVerificationRequest $request)
{
$user = $this->getUser($request);

if (! $user) {
return redirect()->route('backpack.auth.login');
}
pxpm marked this conversation as resolved.
Show resolved Hide resolved

$request->fulfill();

return redirect($this->redirectTo);
Expand All @@ -60,10 +69,28 @@ public function verifyEmail(EmailVerificationRequest $request)
*/
public function resendVerificationEmail(Request $request): \Illuminate\Http\RedirectResponse
{
$request->user(backpack_guard_name())->sendEmailVerificationNotification();
$user = $this->getUser($request);

if(! $user) {
return redirect()->route('backpack.auth.login');
}

$user->sendEmailVerificationNotification();
Alert::success('Email verification link sent successfully.')->flash();

return back()->with('status', 'verification-link-sent');
}

private function getUser(Request $request): ?\Illuminate\Contracts\Auth\MustVerifyEmail
{
return $request->user(backpack_guard_name()) ?? $this->getUserFromCookie();
}

private function getUserFromCookie(): ?\Illuminate\Contracts\Auth\MustVerifyEmail
{
if (Cookie::has('backpack_email_verification')) {
return config('backpack.base.user_model_fqn')::where('email', Cookie::get('backpack_email_verification'))->first();
pxpm marked this conversation as resolved.
Show resolved Hide resolved
}
return null;
}
}
pxpm marked this conversation as resolved.
Show resolved Hide resolved
11 changes: 10 additions & 1 deletion src/app/Http/Requests/EmailVerificationRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
namespace Backpack\CRUD\app\Http\Requests;

use Illuminate\Foundation\Auth\EmailVerificationRequest as OriginalEmailVerificationRequest;
use Illuminate\Support\Facades\Cookie;

class EmailVerificationRequest extends OriginalEmailVerificationRequest
{
public function user($guard = null)
{
return parent::user(backpack_guard_name());
return parent::user(backpack_guard_name()) ?? $this->getUserFromCookie();
}

private function getUserFromCookie()
{
if (Cookie::has('backpack_email_verification')) {
return config('backpack.base.user_model_fqn')::where('email', Cookie::get('backpack_email_verification'))->first();
}
return null;
pxpm marked this conversation as resolved.
Show resolved Hide resolved
}
}
13 changes: 13 additions & 0 deletions src/app/Library/Auth/AuthenticatesUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;
use Illuminate\Support\Facades\Cookie;

trait AuthenticatesUsers
{
Expand Down Expand Up @@ -47,6 +48,18 @@ public function login(Request $request)
}

if ($this->attemptLogin($request)) {
if (config('backpack.base.setup_email_verification_routes', false) ) {
$user = $this->guard()->user();
if ($user->email_verified_at) {
return $this->sendLoginResponse($request);
} else {
$this->guard()->logout();
Cookie::queue('backpack_email_verification', $user->{config('backpack.base.email_column')}, 30);
return $request->wantsJson()
? new Response('Email verification required', 403)
: redirect(route('verification.notice'));
}
}
pxpm marked this conversation as resolved.
Show resolved Hide resolved
return $this->sendLoginResponse($request);
}

Expand Down
11 changes: 8 additions & 3 deletions src/config/backpack/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,16 @@
// (you then need to manually define the routes in your web.php)
'setup_password_recovery_routes' => true,

// Set this to true if you would like to enable email verification for your Authenticable model.
// Make sure your Authenticable model implements the MustVerifyEmail contract and your
// database table contains the email_verified_at column.
// Set this to true if you would like to enable email verification for your user model.
// Make sure your user model implements the MustVerifyEmail contract and your database
// table contains the `email_verified_at` column. Read the following before enabling:
// https://backpackforlaravel.com/docs/6.x/base-how-to#enable-email-verification-in-backpack-routes
'setup_email_verification_routes' => false,

// We will automatically add the Verified middleware to the Backpack midleware group
// if you disable this you must manually add the verified route middleware to
// the routes you would like to be accessed only by verified users.
'add_verified_to_backpack_middleware' => true,
tabacitu marked this conversation as resolved.
Show resolved Hide resolved
pxpm marked this conversation as resolved.
Show resolved Hide resolved

// How many times in any given time period should the user be allowed to
// request a new verification email?
Expand Down
11 changes: 3 additions & 8 deletions src/resources/views/ui/errors/layout.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
@php
// check if user is logged in and verified
$isLoggedInAndVerified = backpack_user() && (config('backpack.base.setup_email_verification_routes', false) ? backpack_user()->hasVerifiedEmail() : true);
@endphp

{{-- show error using sidebar layout if looged in AND on an admin page; otherwise use a blank page --}}
@extends(backpack_view($isLoggedInAndVerified && backpack_theme_config('layout') ? 'layouts.'.backpack_theme_config('layout') : 'errors.blank'))
{{-- show error using sidebar layout if logged in AND on an admin page; otherwise use a blank page --}}
@extends(backpack_view(backpack_user() && backpack_theme_config('layout') ? 'layouts.'.backpack_theme_config('layout') : 'errors.blank'))

@section('content')
<div class="row">
Expand All @@ -17,7 +12,7 @@
<div class="error_title text-muted">
@yield('title')
</div>
@if($isLoggedInAndVerified)
@if(backpack_user())
<div class="error_description text-muted">
<small>
@yield('description')
Expand Down