|
| 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). |
0 commit comments