Skip to content

Commit 1c74553

Browse files
committed
first commit
0 parents  commit 1c74553

16 files changed

+715
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2017 Mehran Rasulian
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

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
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).

composer.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"name": "rasulian/laravel-user-activation",
3+
"description": "A package for user email verification with activation code",
4+
"keywords": [
5+
"laravel",
6+
"user verification",
7+
"token",
8+
"code"
9+
],
10+
"license": "MIT",
11+
"authors": [
12+
{
13+
"name": "Mehran Rasulian",
14+
"email": "[email protected]"
15+
}
16+
],
17+
"require": {},
18+
"autoload": {
19+
"psr-4": {
20+
"Rasulian\\UserVerification\\": "src/"
21+
}
22+
},
23+
"prefer-stable": true
24+
}

config/verification.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
return [
4+
'table_names' => [
5+
'user_verifications' => 'user_verifications'
6+
],
7+
8+
/**
9+
* number of hours that needs to pass before
10+
* we send a now activation email but only if user request it.
11+
*/
12+
'resend_email_after' => 24
13+
];
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class CreateUserVerificationsTable extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::create('user_verifications', function (Blueprint $table) {
16+
$table->integer('user_id')->unsigned();
17+
$table->string('token')->index();
18+
$table->string('code')->index();
19+
$table->timestamps();
20+
});
21+
22+
// Add the verify column to the users table
23+
Schema::table(config('verification.table_names.users'), function (Blueprint $table) {
24+
$table->boolean('verified')->default(false);
25+
});
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*
31+
* @return void
32+
*/
33+
public function down()
34+
{
35+
Schema::table(config('verification.table_names.users'), function (Blueprint $table) {
36+
$table->dropColumn('verified');
37+
});
38+
39+
Schema::drop('user_verifications');
40+
}
41+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rasulian\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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rasulian\UserVerification\Exceptions;
4+
5+
use Exception;
6+
7+
class UserNotVerifiedException extends Exception
8+
{
9+
/**
10+
* The exception description.
11+
*
12+
* @var string
13+
*/
14+
protected $message = 'This user is not verified.';
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rasulian\UserVerification\Exceptions;
4+
5+
use Exception;
6+
7+
class VerifyCodeMismatchException extends Exception
8+
{
9+
/**
10+
* The exception description.
11+
*
12+
* @var string
13+
*/
14+
protected $message = 'Wrong verification token/code.';
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rasulian\UserVerification\Exceptions;
4+
5+
use Exception;
6+
7+
class VerifyTokenMismatchException extends Exception
8+
{
9+
/**
10+
* The exception description.
11+
*
12+
* @var string
13+
*/
14+
protected $message = 'Wrong verification token/code.';
15+
}

src/Facade/Verification.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Rasulian\UserVerification\Facade;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
7+
class Verification extends Facade
8+
{
9+
protected static function getFacadeAccessor()
10+
{
11+
return 'user.verification';
12+
}
13+
}

0 commit comments

Comments
 (0)