|
| 1 | +# laravel-user-verification |
| 2 | +A simple package to activate the users by token and code number. |
| 3 | + |
| 4 | +This package allows you to verify the users either by token to be sent by email and code number for SMS. |
| 5 | + |
| 6 | +## Installation |
| 7 | +This package can be used in Laravel 5.4 or higher. |
| 8 | + |
| 9 | +You can install the package via composer: |
| 10 | +```bash |
| 11 | +composer require rasulian/laravel-user-verification |
| 12 | +``` |
| 13 | + |
| 14 | +In Laravel 5.5 the service provider will automatically get registered. In older versions of the framework just add the service provider in config/app.php file: |
| 15 | +```php |
| 16 | +'providers' => [ |
| 17 | + // ... |
| 18 | + Rasulian\Verification\VerificationServiceProvider::class, |
| 19 | +]; |
| 20 | +``` |
| 21 | + |
| 22 | +You may add the following aliases to your config/app.php: |
| 23 | +```php |
| 24 | +'aliases' => [ |
| 25 | + // ... |
| 26 | + 'Verification' => Rasulian\UserVerification\Facades\Verification::class, |
| 27 | +``` |
| 28 | + |
| 29 | +Publish the package config and database migration file by running the following command: |
| 30 | +```bash |
| 31 | +php artisan vendor:publish --provider="Rasulian\Verification\VerificationServiceProvider::class" |
| 32 | +``` |
| 33 | + |
| 34 | +## Configuration |
| 35 | + |
| 36 | +### Migration |
| 37 | +The table representing the user model must be updated with the `verified` column. This update will be performed by the migration included with this package. |
| 38 | + |
| 39 | +**Please make sure that you don't have the this column on your user table.** |
| 40 | + |
| 41 | +If your user table name is not `users`, you may change that in the `config/verification.php`. |
| 42 | + |
| 43 | +Now you can migrate the normal way you do: |
| 44 | +```bash |
| 45 | +php artisan migrate |
| 46 | +``` |
| 47 | + |
| 48 | +### Middleware |
| 49 | +This package provides an optional middleware throwing `UserVerifiedMiddleware`. |
| 50 | +Please refer to the [Laravel Documentation](https://laravel.com/docs/master/errors#the-exception-handler) to learn more |
| 51 | +about how to work with the exception handler. |
| 52 | + |
| 53 | +To register the default middleware add the following lines to the `$routeMiddleware` array within the `app/Http/Kernel.php` file: |
| 54 | +```php |
| 55 | +protected $routeMiddleware = [ |
| 56 | + 'user.verified' => \Rasulian\UserVerification\Middlewares\UserVerifiedMiddleware::class, |
| 57 | +]; |
| 58 | +``` |
| 59 | + |
| 60 | +You may use this middleware for the routes that needs the user's email or phone number be verified: |
| 61 | +```php |
| 62 | +Route::middleware('auth', 'user.verified')->group(function () { |
| 63 | + // Routes here |
| 64 | +}); |
| 65 | +``` |
| 66 | + |
| 67 | +### Errors |
| 68 | +This package throws several exception. you may use `try\catch` statement or the Laravel exception handler. |
| 69 | + |
| 70 | +* `UserIsVerifiedException` The given user is already verified |
| 71 | +* `UserNotVerifiedException` The given user is not verified |
| 72 | +* `VerifyTokenMismatchException` The given token is wrong or not available |
| 73 | +* `VerifyCodeMismatchException` The given code is wrong or not available |
| 74 | + |
| 75 | +## Usage |
| 76 | + |
| 77 | +### Route |
| 78 | +By default this package provide one route to verify the user by token. |
| 79 | +```php |
| 80 | +Route::get('user/verification/{token}', 'App\Http\Controllers\Auth\RegisterController@verifyUser') |
| 81 | + ->name('user.verify'); |
| 82 | +``` |
| 83 | + |
| 84 | +#### Overriding package route |
| 85 | +To define your own custom routes, put the package service provider call before the `RouteServiceProvider` call in the `config/app.php` file. |
| 86 | +```php |
| 87 | +/* |
| 88 | + * Package Service Providers... |
| 89 | + */ |
| 90 | +Rasulian\UserVerification\VerificationServiceProvider::class, |
| 91 | + |
| 92 | +/* |
| 93 | + * Application Service Providers... |
| 94 | + */ |
| 95 | +App\Providers\RouteServiceProvider::class, |
| 96 | +``` |
| 97 | +Then, add your custom route in your route file. |
| 98 | + |
| 99 | +### Facade |
| 100 | +The package offers a facade Verification::. |
| 101 | + |
| 102 | +### verification Config file |
| 103 | +After publishing the package config, it will be located at the `config` directory. You are free to change the table name |
| 104 | +for the `user` and the `user_verifications` which represents the fields for storing the token and code. |
| 105 | + |
| 106 | +```php |
| 107 | +<?php |
| 108 | + |
| 109 | +return [ |
| 110 | + 'table_names' => [ |
| 111 | + 'users' => 'users', |
| 112 | + 'user_verifications' => 'user_verifications' |
| 113 | + ], |
| 114 | + |
| 115 | + /** |
| 116 | + * number of hours that needs to pass before |
| 117 | + * we generate a new token\code but only if user request it. |
| 118 | + */ |
| 119 | + 'generate_after' => 24 |
| 120 | +]; |
| 121 | +``` |
| 122 | + |
| 123 | +### How to use the package |
| 124 | +This package is written as simple as possible. |
| 125 | +It creates a token and code for the user and verifies the user either by token or code. |
| 126 | + |
| 127 | +Here is a sample on how to generate a token, send it as an email and verify it. |
| 128 | + |
| 129 | +Edit the `App\Http\Auth\RegisterController` file: |
| 130 | + |
| 131 | +```php |
| 132 | +<?php |
| 133 | + |
| 134 | +namespace App\Http\Controllers\Auth; |
| 135 | + |
| 136 | +use App\Mail\Welcome; |
| 137 | +use App\Http\Controllers\Controller; |
| 138 | +use Illuminate\Http\Request; |
| 139 | +use Mail; |
| 140 | +use Rasulian\UserVerification\Facades\Verification; |
| 141 | + |
| 142 | +class RegisterController extends Controller |
| 143 | +{ |
| 144 | + // |
| 145 | + // Code |
| 146 | + // |
| 147 | + |
| 148 | + /** |
| 149 | + * Create a new controller instance. |
| 150 | + * |
| 151 | + * @return void |
| 152 | + */ |
| 153 | + public function __construct() |
| 154 | + { |
| 155 | + $this->middleware('guest', ['except' => 'verifyUser']); |
| 156 | + } |
| 157 | + |
| 158 | + // |
| 159 | + // Code |
| 160 | + // |
| 161 | + |
| 162 | + /** |
| 163 | + * The user has been registered. |
| 164 | + * |
| 165 | + * @param \Illuminate\Http\Request $request |
| 166 | + * @param mixed $user |
| 167 | + * @return mixed |
| 168 | + */ |
| 169 | + protected function registered(Request $request, $user) |
| 170 | + { |
| 171 | + $token = Verification::getVerificationToken($user); |
| 172 | + $url = route('user.verify', $token); |
| 173 | + Mail::to($user->email)->send(new Welcome($url)); |
| 174 | + |
| 175 | + return redirect('/home')->with('success', 'Registered. Verify your email!'); |
| 176 | + } |
| 177 | + |
| 178 | + public function verifyUser($token) |
| 179 | + { |
| 180 | + $user = Verification::verifyUserByToken($token); |
| 181 | + |
| 182 | + if (auth()->guest()) |
| 183 | + auth()->login($user); |
| 184 | + |
| 185 | + return redirect('/home')->with('success', 'Your email verified successfully!'); |
| 186 | + } |
| 187 | +} |
| 188 | +``` |
| 189 | + |
| 190 | +Here we use the `registered` method to create token and send it, |
| 191 | +which will overrides `\Illuminate\Foundation\Auth\RegistersUsers@registered` method. |
| 192 | +We get a token for the given user, make it as a url and send it as an email. |
| 193 | + |
| 194 | +If the user clicks on the link, the `verifyUser` method will be executed. |
| 195 | +Here we just verify the user by the given token. |
| 196 | + |
| 197 | +**Please make sure that you add the `verifyUser` to the `except` array of the `guest` middleware in the constructor.** |
| 198 | + |
| 199 | +#### Relaunch the process |
| 200 | +If you want to regenerate and resend the verification token, you can do this with the `getVerificationToken` method. |
| 201 | + |
| 202 | +The generate method will generate a new token for the given user and change the `verified` column to `false`. |
| 203 | + |
| 204 | +## CONTRIBUTE |
| 205 | +Feel free to comment, contribute and help. 1 PR = 1 feature. |
| 206 | + |
| 207 | +## LICENSE |
| 208 | +Laravel User Verification is licensed under [The MIT License (MIT)](https://github.com/mehranrasulian/laravel-user-verification/blob/master/LICENSE). |
0 commit comments