Skip to content

Commit 7a6deef

Browse files
committed
Refactor + simpler token verification process
1 parent bbc26f2 commit 7a6deef

File tree

6 files changed

+167
-139
lines changed

6 files changed

+167
-139
lines changed

README.md

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ resources/views/emails/user-verification.blade.php. The view will receive the
117117
token. Here is an sample e-mail view to get you started:
118118

119119
```
120-
Click here to verify your account {{ url('auth/verification/' . $user->verification_token) }}
120+
Click here to verify your account: <a href="{{ $link = url('verification', $user->verification_token) . '?email=' . urlencode($user->email) }}"> {{ $link }}</a>
121121
```
122122

123123
## Usage
124124

125125
### API
126126

127-
The package public API offers four (4) methods.
127+
The package public API offers three (3) methods.
128128

129129
* `generate(AuthenticatableContract $user)`
130130

@@ -138,14 +138,10 @@ Send by email a link containing the verification token.
138138

139139
Process the token verification for the given user.
140140

141-
* `isVerified(AuthenticatableContract $user)`
142-
143-
Check if the given user is verified.
144-
145141
### Facade
146142

147143
The package offers a facade callable with `UserVerification::`. You can use it
148-
over the four (4) previous listed public methods.
144+
over the three (3) previous listed public methods.
149145

150146
### Trait
151147

@@ -156,7 +152,7 @@ The package also offers a trait for a quick implementation.
156152
The two following methods are endpoints you can join by defining the proper
157153
route(s) of your choice.
158154

159-
* `getVerification($token)`
155+
* `getVerification(Request $request, $token)`
160156

161157
Handle the user verification. It requires a string parameter representing the
162158
verification token to verify.
@@ -281,14 +277,10 @@ Edit the `app\Http\Controller\Auth\AuthController.php` file.
281277

282278
Edit the `app\Http\routes.php` file.
283279

284-
- Define two new routes
280+
- Define two new routes. Routes are customizable.
281+
Don't forget to update the previous listed redirect attributes.
285282

286283
```
287-
Route::get('auth/verification/error', 'Auth\AuthController@getVerificationError');
288-
Route::get('auth/verification/{token}', 'Auth\AuthController@getVerification');
289-
290-
or
291-
292284
Route::get('verification/error', 'Auth\AuthController@getVerificationError');
293285
Route::get('verification/{token}', 'Auth\AuthController@getVerification');
294286
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Jrean\UserVerification\Exceptions;
4+
5+
use Exception;
6+
7+
class TokenMismatchException extends Exception
8+
{
9+
/**
10+
* The exception description.
11+
*
12+
* @var string
13+
*/
14+
protected $message = 'Wrong verification token.';
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Jrean\UserVerification\Exceptions;
4+
5+
use Exception;
6+
7+
class UserIsVerifiedException extends Exception
8+
{
9+
/**
10+
* The exception description.
11+
*
12+
* @var string
13+
*/
14+
protected $message = 'This user is already verified.';
15+
}

src/Traits/RedirectsUsers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,6 @@ public function redirectAfterVerification()
4141
*/
4242
public function redirectIfVerificationFails()
4343
{
44-
return property_exists($this, 'redirectIfVerificationFails') ? $this->redirectIfVerificationFails : '/auth/verification/error';
44+
return property_exists($this, 'redirectIfVerificationFails') ? $this->redirectIfVerificationFails : '/verification/error';
4545
}
4646
}

src/Traits/VerifiesUsers.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace Jrean\UserVerification\Traits;
44

5+
use Illuminate\Http\Request;
6+
use Illuminate\Support\Facades\Validator;
57
use Jrean\UserVerification\Facades\UserVerification;
8+
use Jrean\UserVerification\Exceptions\UserNotFoundException;
9+
use Jrean\UserVerification\Exceptions\UserIsVerifiedException;
610

711
trait VerifiesUsers
812
{
@@ -14,16 +18,16 @@ trait VerifiesUsers
1418
* @param string $token
1519
* @return Response
1620
*/
17-
public function getVerification($token)
21+
public function getVerification(Request $request, $token)
1822
{
19-
$user = UserVerification::getUser($token, $this->userTable());
23+
$this->validateRequest($request);
2024

21-
if (UserVerification::isVerified($user)) {
22-
return redirect($this->redirectIfVerified());
23-
}
24-
25-
if (! UserVerification::process($user, $token)) {
25+
try {
26+
UserVerification::process($request->input('email'), $token, $this->userTable());
27+
} catch (UserNotFoundException $e) {
2628
return redirect($this->redirectIfVerificationFails());
29+
} catch (UserIsVerifiedException $e) {
30+
return redirect($this->redirectIfVerified());
2731
}
2832

2933
return redirect($this->redirectAfterVerification());
@@ -39,12 +43,29 @@ public function getVerificationError()
3943
return view($this->verificationErrorView());
4044
}
4145

46+
/**
47+
* Validate the verification link.
48+
*
49+
* @param string $token
50+
* @return Response
51+
*/
52+
protected function validateRequest(Request $request)
53+
{
54+
$validator = Validator::make($request->all(), [
55+
'email' => 'required|email'
56+
]);
57+
58+
if ($validator->fails()) {
59+
return redirect($this->redirectIfVerificationFails());
60+
}
61+
}
62+
4263
/**
4364
* Get the verification error view name.
4465
*
4566
* @return string
4667
*/
47-
public function verificationErrorView()
68+
protected function verificationErrorView()
4869
{
4970
return property_exists($this, 'verificationErrorView') ? $this->verificationErrorView : 'errors.user-verification';
5071
}
@@ -54,7 +75,7 @@ public function verificationErrorView()
5475
*
5576
* @return string
5677
*/
57-
public function userTable()
78+
protected function userTable()
5879
{
5980
return property_exists($this, 'userTable') ? $this->userTable : 'users';
6081
}

0 commit comments

Comments
 (0)