Skip to content

Commit 126ba90

Browse files
committed
Documentation
1 parent fb753d7 commit 126ba90

File tree

4 files changed

+329
-7
lines changed

4 files changed

+329
-7
lines changed

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
T License (MIT)
2+
3+
Copyright (c) 2015 Jean Ragouin <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+276
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
**jrean/laravel-user-verification** is a PHP package built for Laravel 5 to
2+
easily handle a user verification flow and validate its email.
3+
4+
## About
5+
6+
- Generate and store a verification token for a registered and authenticated user.
7+
- Send an email with the verification token link.
8+
- Handle the verification.
9+
- Set the user as verified.
10+
11+
## Installation
12+
13+
This project can be installed via [Composer](http://getcomposer.org).
14+
To get the latest version of Laravel User Verification, simply add the following line to
15+
the require block of your composer.json file:
16+
17+
"jrean/laravel-user-verification": "dev-master"
18+
19+
or
20+
21+
"composer require jrean/laravel-user-verification"
22+
23+
You'll then need to run `composer install` or `composer update` to download it and
24+
have the autoloader updated.
25+
26+
### Add the Service Provider
27+
28+
Once Larvel User Verification is installed, you need to register the service provider.
29+
30+
Open up `config/app.php` and add the following to the `providers` key:
31+
32+
* `Jrean\UserVerification\UserVerificationServiceProvider::class`
33+
34+
### Add the Facade/Alias
35+
36+
Open up `config/app.php` and add the following to the `aliases` key:
37+
38+
* `'UserVerification' => Jrean\UserVerification\Facades\UserVerification::class`
39+
40+
## Configuration
41+
42+
Prior to use this package the table representing the user must be updated with
43+
two new columns, "verified" and "verification_token". It is mandatory to add
44+
the two columns on the same table and where the user's email is
45+
stored. Last but not least, the model must implement the authenticatable
46+
interface `Illuminate\Contracts\Auth\Authenticatable` which is the default with
47+
the Eloquent User model.
48+
49+
### Migration
50+
51+
Generate the migration file with the following artisan command:
52+
53+
```
54+
php artisan make:migration add_verification_to_:table_table --table=":table"
55+
```
56+
57+
Where `:table` is replaced by the table of your choice.
58+
59+
For instance if you want to keep the default Eloquent User table:
60+
61+
```
62+
php artisan make:migration add_verification_to_users_table --table="users"
63+
```
64+
65+
Once the migration is generated, edit the file in `database/migration` with the following:
66+
67+
```
68+
/**
69+
* Run the migrations.
70+
*
71+
* @return void
72+
*/
73+
public function up()
74+
{
75+
Schema::table(':table', function (Blueprint $table) {
76+
$table->boolean('verified')->default(false);
77+
$table->string('verification_token')->nullable();
78+
});
79+
}
80+
81+
/**
82+
* Reverse the migrations.
83+
*
84+
* @return void
85+
*/
86+
public function down()
87+
{
88+
Schema::table(':table', function (Blueprint $table) {
89+
$table->dropColumn('verified');
90+
$table->dropColumn('verification_token');
91+
});
92+
}
93+
94+
```
95+
96+
Where `:table` is replaced by the table of your choice.
97+
98+
Migrate the migration with the following command:
99+
100+
```
101+
php artisan migrate
102+
```
103+
104+
### Email
105+
106+
This package offers to send an email with a link containing the verification token.
107+
108+
Please refer to the Laravel documentation for the proper email component configuration.
109+
110+
[Laravel Email](http://laravel.com/docs/5.1/mail)
111+
112+
#### View
113+
114+
When a user submits a request to register or when you decide, they will receive
115+
an e-mail with a link that points to the getVerification method (typically
116+
routed at /auth/verification/{token}) of the AuthController. You will need to
117+
create a view for this e-mail at
118+
resources/views/emails/user-verification.blade.php. The view will receive the
119+
$model variable which contains the user information such as the verification
120+
token. Here is an example e-mail view to get you started:
121+
122+
```
123+
Click here to verify your account {{ url('auth/verification/' . $model->verification_token) }}
124+
```
125+
126+
## Usage
127+
128+
### API
129+
130+
The package public API offers four (4) methods.
131+
132+
* `generate(AuthenticatableContract $model)`
133+
134+
Generate and save a verification token the given user.
135+
136+
* `send(AuthenticatableContract $model)`
137+
138+
Send by email a link containing the verification token.
139+
140+
* `process(AuthenticatableContract $model, $token)`
141+
142+
Process the token verification for the given user.
143+
144+
* `isVerified(AuthenticatableContract $model)`
145+
146+
Check if the given user is verified.
147+
148+
### Facade
149+
150+
The package offers a facade callable with `UserVerification::`. You can use it
151+
over the four (4) previous listed public methods.
152+
153+
### Trait
154+
155+
The package also offers a trait for a quick implementation.
156+
The trait contains three (3) public methods and four (4) protected
157+
attributes/properties. Feel free to extend/overwrite/customize.
158+
159+
#### Actions
160+
161+
The three following methods are endpoints you can join by defining the proper
162+
route(s) of your choice.
163+
164+
* `getVerificationToken()`
165+
166+
Handle the verification token generation.
167+
168+
* `getVerification($token)`
169+
170+
Handle the user verification. It requires a string parameter that should
171+
represent the verification token to verify.
172+
173+
* `getVerificationError()`
174+
175+
Do something if the verification fails.
176+
177+
#### Attributes/Properties
178+
179+
The five (5) following protected attributes/properties should be extended and customized
180+
to your needs.
181+
182+
* `$redirectIfVerified = '/';`
183+
184+
Where to reditect if the authenticated user is already verified.
185+
186+
* `$redirectAfterTokenGeneration = '/';`
187+
188+
Where to redirect after a successful verification token generation.
189+
190+
* `$redirectAfterVerification = '/';`
191+
192+
Where to redirect after a successful verification token verification.
193+
194+
* `$redirectIfVerificationFails = '/auth/verification/error';`
195+
196+
Where to redirect after a failling verification token verification.
197+
198+
* `$verifiationErrorView = '';`
199+
200+
Name of the view returned by the getVerificationError method.
201+
202+
## Example
203+
204+
This package whishes to let you be creative while offering you a predefined
205+
path. The following examples assume you have configured Laravel for the
206+
package as well as created and migrated the migration according to this
207+
documentation.
208+
209+
### Example 1
210+
211+
The following code sample aims to showcase a quick and basic implementation.
212+
213+
Edit the `app\Http\Controller\Auth\AuthController.php` file.
214+
215+
- Import the `VerifiesUsers` trait
216+
- Overwrite and customize the redirect path attributes/properties
217+
- Overwrite and customize the view name for the getVerificationError method
218+
- Create the view according to the defined path.
219+
- Overwrite the postRegister method
220+
221+
```
222+
...
223+
224+
use Jrean\UserVerification\Facades\UserVerification;
225+
226+
...
227+
228+
use VerifiesUsers;
229+
230+
...
231+
232+
/**
233+
* Handle a registration request for the application.
234+
*
235+
* @param \Illuminate\Http\Request $request
236+
* @return \Illuminate\Http\Response
237+
*/
238+
public function postRegister(Request $request)
239+
{
240+
$validator = $this->validator($request->all());
241+
242+
if ($validator->fails()) {
243+
$this->throwValidationException(
244+
$request, $validator
245+
);
246+
}
247+
248+
$user = $this->create($request->all());
249+
250+
Auth::login($user);
251+
252+
UserVerification::generate($user);
253+
254+
UserVerification::send($user);
255+
256+
return redirect($this->redirectPath());
257+
}
258+
```
259+
260+
Edit the `app\Http\routes.php` file.
261+
262+
- Define two new routes
263+
264+
```
265+
Route::get('auth/verification/error', 'Auth\AuthController@getVerificationError');
266+
Route::get('auth/verification/{token}', 'Auth\AuthController@getVerification');
267+
```
268+
269+
## Contribute
270+
271+
This package is (yet) under active development and refactoring.
272+
Please, feel free to comment, contribute and help. Units testing are on the way.
273+
274+
## License
275+
276+
Laravel User Verification is licensed under [The MIT License (MIT)](LICENSE).

src/UserVerification.php

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@
22

33
namespace Jrean\UserVerification;
44

5-
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
6-
75
use Illuminate\Contracts\Mail\Mailer as MailerContract;
86
use Illuminate\Database\Schema\Builder;
7+
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
98
use Jrean\UserVerification\VerificationException;
109

1110
class UserVerification
@@ -27,8 +26,8 @@ class UserVerification
2726
/**
2827
* Constructor.
2928
*
30-
* @param \Illuminate\Contracts\Mail\Mailer $mailer
31-
* @param \Illuminate\Database\Schema\Builder $schema
29+
* @param \Illuminate\Contracts\Mail\Mailer $mailer
30+
* @param \Illuminate\Database\Schema\Builder $schema
3231
*
3332
* @return void
3433
*/
@@ -40,7 +39,7 @@ public function __construct(MailerContract $mailer, Builder $schema)
4039
}
4140

4241
/**
43-
* Generate a verification token for that user.
42+
* Generate and save a verification token the given user.
4443
*
4544
* @param \Illuminate\Contracts\Auth\Authenticatable $model
4645
* @return boolean
@@ -51,7 +50,7 @@ public function generate(AuthenticatableContract $model)
5150
}
5251

5352
/**
54-
* Send by email the verification token.
53+
* Send by email a link containing the verification token.
5554
*
5655
* @param \Illuminate\Contracts\Auth\Authenticatable $model
5756
* @returnt boolean

src/VerifiesUsers.php

+27-1
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,40 @@
66

77
trait VerifiesUsers
88
{
9+
/**
10+
* Where to reditect if the user is already verified.
11+
*
12+
* @var string
13+
*/
914
protected $redirectIfVerified = '/';
1015

16+
/**
17+
* Where to redirect after a successful verification token generation.
18+
*
19+
* @var string
20+
*/
1121
protected $redirectAfterTokenGeneration = '/';
1222

23+
/**
24+
* Where to redirect after a successful verification token verification.
25+
*
26+
* @var string
27+
*/
1328
protected $redirectAfterVerification = '/';
1429

30+
/**
31+
* Where to redirect after a failling verification token verification.
32+
*
33+
* @var string
34+
*/
1535
protected $redirectIfVerificationFails = '/auth/verification/error';
1636

37+
/**
38+
* Name of the view returned by the getVerificationError method.
39+
*
40+
* @var mixed
41+
*/
42+
protected $verificationErrorView = '';
1743

1844
/**
1945
* Handle the verification token generation.
@@ -62,7 +88,7 @@ public function getVerificationError()
6288
$user = auth()->user();
6389

6490
if ( ! UserVerification::isVerified($user)) {
65-
//
91+
return view($this->verificationErrorView);
6692
}
6793

6894
return redirect($this->redirectIfVerified);

0 commit comments

Comments
 (0)