Skip to content

Commit 0840da3

Browse files
committed
Allow users to resend the verification mail
1 parent ccdf413 commit 0840da3

File tree

7 files changed

+168
-48
lines changed

7 files changed

+168
-48
lines changed

README.md

+38-10
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ The Laravel Email Verification package is built for Laravel 5.4 to easily handle
1414
- [x] Event based: No need to override your `register()` method.
1515
- [x] Using the Laravel 5.3 notification system.
1616
- [x] Allow certain routes for verified users only using the `IsEmailVerified` middleware.
17-
- [x] Resend the verification email anytime.
18-
17+
- [x] Let the users resend the verification email at anytime.
18+
- [x] Ready for Localization.
1919

2020

2121
## Configuration
@@ -53,7 +53,7 @@ If this key is not found, the default `App\User` will be used to get the table n
5353
To customize the migration, publish it with the following command:
5454

5555
```
56-
php artisan vendor:publish --provider="Lunaweb\EmailVerification\EmailVerificationServiceProvider" --tag="migrations"
56+
php artisan vendor:publish --provider="Lunaweb\EmailVerification\Providers\EmailVerificationServiceProvider" --tag="migrations"
5757
```
5858

5959
### User Model
@@ -91,7 +91,8 @@ class RegisterController extends Controller
9191

9292
public function __construct()
9393
{
94-
$this->middleware('guest', ['except' => ['verify']]);
94+
$this->middleware('guest', ['except' => ['verify', 'showResendVerificationEmailForm', 'resendVerificationEmail']]);
95+
$this->middleware('auth', ['only' => ['showResendVerificationEmailForm', 'resendVerificationEmail']]);
9596
}
9697

9798
// ...
@@ -102,10 +103,15 @@ class RegisterController extends Controller
102103

103104
There is no need to override `register()`. As default, the package listens for the `Illuminate\Auth\Events\Registered` event and sends the verification mail. You can disable this behavior using the `listen_registered_event` setting.
104105

105-
The package adds one route vor the email verification link.
106+
### Routes
107+
108+
The package adds the following routes.
106109

107110
```php
108-
Route::get('register/verify', 'App\Http\Controllers\Auth\RegisterController@verify');
111+
Route::get('register/verify', 'App\Http\Controllers\Auth\RegisterController@verify')->name('verifyEmailLink');
112+
Route::get('register/verify/resend', 'App\Http\Controllers\Auth\RegisterController@showResendVerificationEmailForm')->name('showResendVerificationEmailForm');
113+
Route::post('register/verify/resend', 'App\Http\Controllers\Auth\RegisterController@resendVerificationEmail')->name('resendVerificationEmail');
114+
109115
```
110116

111117
### Middleware
@@ -133,6 +139,22 @@ The package emits 2 events:
133139
* ``Lunaweb\EmailVerification\Events\EmailVerificationSent``
134140
* ``Lunaweb\EmailVerification\Events\UserVerified``
135141

142+
143+
144+
### Resend the verification mail
145+
146+
Using the `isEmailVerified` Middleware, the following form is shown to the user. It allows the user to correct his email address and resend the verification mail.
147+
148+
![Screenshot](https://user-images.githubusercontent.com/1945577/27735164-7b316630-5d9e-11e7-86f6-8922a2488cfb.png)
149+
150+
You can manually point the user to this form using the `showResendVerificationEmailForm` route (Default: `register/verify/resend`).
151+
152+
To programmatically resend the verification mail:
153+
```php
154+
$this->app->make('Lunaweb\EmailVerification\EmailVerification')->sendVerifyLink($user);
155+
```
156+
157+
136158
### Customize the verification mail
137159

138160
Therefore, override `sendEmailVerificationNotification()` of your User model. Example:
@@ -157,8 +179,14 @@ class User implements CanVerifyEmailContract
157179
}
158180
```
159181

160-
### Resend the verification mail
182+
### Customize the resend form
183+
```
184+
php artisan vendor:publish --provider="Lunaweb\EmailVerification\Providers\EmailVerificationServiceProvider" --tag="views"
185+
```
186+
The template can be found in `resources/views/vendor/emailverification/resend.blade.php`
161187

162-
```php
163-
$this->app->make('Lunaweb\EmailVerification\EmailVerification')->sendVerifyLink($user);
164-
```
188+
### Customize the messages / localization
189+
```
190+
php artisan vendor:publish --provider="Lunaweb\EmailVerification\Providers\EmailVerificationServiceProvider" --tag="translations"
191+
```
192+
The localization files can be found in `resources/lang/vendor/emailverification`

resources/lang/en/messages.php

+7
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,12 @@
1010
'sent' => 'We have e-mailed your account activation link!',
1111
'token' => 'This email verification token is invalid.',
1212
'user' => "We can't find a user with that e-mail address.",
13+
'resend' => [
14+
'title' => 'Email Verification',
15+
'warning' => 'We have sent a verification mail to <strong>:email</strong>. Please activate your account with the link in this mail. If you cannot find the mail, please also check the Junk/Spam folder!',
16+
'instructions' => 'If you have not received a verification email or if you have mistyped your email address, you can resend the verification mail.',
17+
'email' => 'Email',
18+
'submit' => 'Resend'
19+
]
1320

1421
];

resources/views/resend.blade.php

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
@extends('layouts.app')
2+
3+
@section('content')
4+
<div class="container">
5+
<div class="row">
6+
<div class="col-md-8 col-md-offset-2">
7+
<div class="panel panel-default">
8+
<div class="panel-heading">@lang('emailverification::messages.resend.title')</div>
9+
<div class="panel-body">
10+
11+
@if($verified)
12+
<div class="alert alert-success">
13+
@lang('emailverification::messages.done')
14+
</div>
15+
@else
16+
17+
<form class="form-horizontal" role="form" method="POST"
18+
action="{{ route('resendVerificationEmail') }}">
19+
{!! csrf_field() !!}
20+
21+
22+
<div class="alert alert-warning dark">
23+
@lang('emailverification::messages.resend.warning', ['email' => $email])
24+
</div>
25+
26+
<p>
27+
@lang('emailverification::messages.resend.instructions')
28+
</p>
29+
30+
<div class="form-group{{ $errors->has('email') ? ' has-error' : '' }}">
31+
<label class="col-md-4 control-label">@lang('emailverification::messages.resend.email')</label>
32+
33+
<div class="col-md-6">
34+
35+
<input type="text" class="form-control" name="email"
36+
value="{{ old('email', $email) }}">
37+
38+
@if ($errors->has('email'))
39+
<span class="help-block">
40+
<strong>{{ $errors->first('email') }}</strong>
41+
</span>
42+
@endif
43+
</div>
44+
</div>
45+
46+
47+
<div class="form-group">
48+
49+
50+
<div class="col-md-6 col-md-offset-4">
51+
<button type="submit" class="btn btn-primary">
52+
@lang('emailverification::messages.resend.submit')
53+
</button>
54+
</div>
55+
</div>
56+
57+
58+
</form>
59+
60+
61+
@endif
62+
</div>
63+
</div>
64+
</div>
65+
</div>
66+
</div>
67+
@endsection

src/Http/routes.php

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
// Verification
55
Route::get('register/verify', 'App\Http\Controllers\Auth\RegisterController@verify')->name('verifyEmailLink');
6+
Route::get('register/verify/resend', 'App\Http\Controllers\Auth\RegisterController@showResendVerificationEmailForm')->name('showResendVerificationEmailForm');
7+
Route::post('register/verify/resend', 'App\Http\Controllers\Auth\RegisterController@resendVerificationEmail')->name('resendVerificationEmail');
68

79

810

src/Middleware/IsEmailVerified.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class IsEmailVerified
2626
public function handle($request, Closure $next)
2727
{
2828
if( !is_null($request->user()) && !$request->user()->verified){
29-
throw new UserNotVerifiedException;
29+
return redirect(route('showResendVerificationEmailForm'));
3030
}
3131

3232
return $next($request);

src/Providers/EmailVerificationServiceProvider.php

+10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ public function boot()
2626
*/
2727
$this->loadRoutesFrom(__DIR__ . '/../Http/routes.php');
2828

29+
30+
/*
31+
* Views
32+
*/
33+
$this->loadViewsFrom(__DIR__ . '/../../resources/views', 'emailverification');
34+
$this->publishes([
35+
__DIR__ . '/../../resources/views' => resource_path('views/vendor/emailverification'),
36+
], 'views');
37+
38+
2939
/*
3040
* Migrations
3141
*/

src/Traits/VerifiesEmail.php

+43-37
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Lunaweb\EmailVerification\Traits;
99

1010
use Illuminate\Foundation\Auth\RedirectsUsers;
11+
use Illuminate\Support\Facades\Session;
1112
use Illuminate\Support\Str;
1213
use Illuminate\Http\Request;
1314
use Illuminate\Support\Facades\Auth;
@@ -17,8 +18,6 @@ trait VerifiesEmail
1718
{
1819

1920

20-
21-
2221
/**
2322
* Verifies the given user's email.
2423
*
@@ -28,62 +27,68 @@ trait VerifiesEmail
2827
*/
2928
public function verify(Request $request, EmailVerification $emailVerification)
3029
{
31-
$this->validate($request, $this->rules(), $this->validationErrorMessages());
32-
// Here we will attempt to reset the user's password. If it is successful we
33-
// will update the password on an actual user model and persist it to the
30+
$this->validate($request, [
31+
'token' => 'required',
32+
'email' => 'required|email',
33+
'expiration' => 'required|date_format:U'
34+
], []);
35+
// Here we will attempt to verify the user. If it is successful we
36+
// will update the verified on an actual user model and persist it to the
3437
// database. Otherwise we will parse the error and return the response.
3538
$response = $emailVerification->verify(
36-
$this->credentials($request), function ($user) {
39+
$request->only(
40+
'email', 'expiration', 'token'
41+
), function ($user) {
3742
$this->verifiedEmail($user);
3843
}
3944
);
4045

4146

42-
// If the password was successfully reset, we will redirect the user back to
47+
// If the user was successfully verified, we will redirect the user back to
4348
// the application's home authenticated view. If there is an error we can
4449
// redirect them back to where they came from with their error message.
4550
return $response == EmailVerification::VERIFIED
4651
? $this->sendVerificationResponse($response)
4752
: $this->sendVerificationFailedResponse($request, $response);
4853
}
54+
55+
4956
/**
50-
* Get the password reset validation rules.
51-
*
52-
* @return array
53-
*/
54-
protected function rules()
55-
{
56-
return [
57-
'token' => 'required',
58-
'email' => 'required|email',
59-
'expiration' => 'required|date_format:U'
60-
];
61-
}
62-
/**
63-
* Get the password reset validation error messages.
57+
* Show form to the user which allows resending the verification mail
6458
*
65-
* @return array
59+
* @return \Illuminate\Http\RedirectResponse
6660
*/
67-
protected function validationErrorMessages()
61+
public function showResendVerificationEmailForm()
6862
{
69-
return [];
63+
$user = Auth::user();
64+
return view('emailverification::resend', ['verified' => $user->verified, 'email' => $user->email]);
7065
}
66+
7167
/**
72-
* Get the email verification credentials from the request.
68+
* Resend the verification mail
7369
*
74-
* @param \Illuminate\Http\Request $request
75-
* @return array
70+
* @param Request $request
71+
* @return \Illuminate\Http\RedirectResponse
7672
*/
77-
protected function credentials(Request $request)
73+
public function resendVerificationEmail(Request $request)
7874
{
79-
return $request->only(
80-
'email', 'expiration', 'token'
81-
);
75+
$this->validate($request, [
76+
'email' => 'required|email|max:255'
77+
]);
78+
$user = Auth::user();
79+
$user->email = $request->email;
80+
$user->save();
81+
82+
$sent = resolve('Lunaweb\EmailVerification\EmailVerification')->sendVerifyLink($user);
83+
Session::flash($sent == EmailVerification::VERIFY_LINK_SENT ? 'success' : 'error', trans($sent));
84+
85+
return redirect($this->redirectPath());
8286
}
87+
8388
/**
8489
* Store the user's verification
8590
*
86-
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
91+
* @param \Illuminate\Contracts\Auth\CanResetPassword $user
8792
* @return void
8893
*/
8994
protected function verifiedEmail($user)
@@ -93,21 +98,23 @@ protected function verifiedEmail($user)
9398
])->save();
9499
$this->guard()->login($user);
95100
}
101+
96102
/**
97-
* Get the response for a successful password reset.
103+
* Get the response for a successful user verification.
98104
*
99-
* @param string $response
105+
* @param string $response
100106
* @return \Illuminate\Http\RedirectResponse
101107
*/
102108
protected function sendVerificationResponse($response)
103109
{
104110
return redirect($this->redirectPath())->with('success', trans($response));
105111
}
112+
106113
/**
107-
* Get the response for a failed password reset.
114+
* Get the response for a failed user verification.
108115
*
109116
* @param \Illuminate\Http\Request
110-
* @param string $response
117+
* @param string $response
111118
* @return \Illuminate\Http\RedirectResponse
112119
*/
113120
protected function sendVerificationFailedResponse(Request $request, $response)
@@ -116,5 +123,4 @@ protected function sendVerificationFailedResponse(Request $request, $response)
116123
}
117124

118125

119-
120126
}

0 commit comments

Comments
 (0)