2017 © Pedro Peláez
 

library laravel-user-verification

A package for user email and phone number verification with activation code

image

rasulian/laravel-user-verification

A package for user email and phone number verification with activation code

  • Saturday, October 14, 2017
  • by rasulian
  • Repository
  • 1 Watchers
  • 2 Stars
  • 19 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 2 Versions
  • 12 % Grown

The README.md

laravel-user-verification

A simple package to activate the users by token and code number., (*1)

This package allows you to verify the users either by token to be sent by email and code number for SMS., (*2)

Installation

This package can be used in Laravel 5.4 or higher., (*3)

You can install the package via composer:, (*4)

composer require rasulian/laravel-user-verification

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:, (*5)

'providers' => [
    // ...
    Rasulian\Verification\VerificationServiceProvider::class,
];

You may add the following aliases to your config/app.php:, (*6)

'aliases' => [
    // ...
    'Verification' => Rasulian\UserVerification\Facades\Verification::class,

Publish the package config and database migration file by running the following command:, (*7)

php artisan vendor:publish --provider="Rasulian\Verification\VerificationServiceProvider::class"

Configuration

Migration

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., (*8)

Please make sure that you don't have the this column on your user table., (*9)

If your user table name is not users, you may change that in the config/verification.php., (*10)

Now you can migrate the normal way you do:, (*11)

php artisan migrate

Middleware

This package provides an optional middleware throwing UserVerifiedMiddleware. Please refer to the Laravel Documentation to learn more about how to work with the exception handler., (*12)

To register the default middleware add the following lines to the $routeMiddleware array within the app/Http/Kernel.php file:, (*13)

protected $routeMiddleware = [
    'user.verified' => \Rasulian\UserVerification\Middlewares\UserVerifiedMiddleware::class,
];

You may use this middleware for the routes that needs the user's email or phone number be verified:, (*14)

Route::middleware('auth', 'user.verified')->group(function () {
    // Routes here
});

Errors

This package throws several exception. you may use try\catch statement or the Laravel exception handler., (*15)

  • UserIsVerifiedException The given user is already verified
  • UserNotVerifiedException The given user is not verified
  • VerifyTokenMismatchException The given token is wrong or not available
  • VerifyCodeMismatchException The given code is wrong or not available

Usage

Route

By default this package provide one route to verify the user by token., (*16)

Route::get('user/verification/{token}', 'App\Http\Controllers\Auth\RegisterController@verifyUser')
    ->name('user.verify');

Overriding package route

To define your own custom routes, put the package service provider call before the RouteServiceProvider call in the config/app.php file., (*17)

/*
 * Package Service Providers...
 */
Rasulian\UserVerification\VerificationServiceProvider::class,

/*
 * Application Service Providers...
 */
App\Providers\RouteServiceProvider::class,

Then, add your custom route in your route file., (*18)

Facade

The package offers a facade Verification::., (*19)

verification Config file

After publishing the package config, it will be located at the config directory. You are free to change the table name for the user and the user_verifications which represents the fields for storing the token and code., (*20)

<?php

return [
    'table_names' => [
        'users' => 'users',
        'user_verifications' => 'user_verifications'
    ],

    /**
     * number of hours that needs to pass before
     * we generate a new token\code but only if user request it.
     */
    'generate_after' => 24
];

How to use the package

This package is written as simple as possible. It creates a token and code for the user and verifies the user either by token or code., (*21)

Here is a sample on how to generate a token, send it as an email and verify it., (*22)

Edit the App\Http\Auth\RegisterController file:, (*23)

<?php

namespace App\Http\Controllers\Auth;

use App\Mail\Welcome;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Mail;
use Rasulian\UserVerification\Facades\Verification;

class RegisterController extends Controller
{
    //
    // Code
    //

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest', ['except' => 'verifyUser']);
    }

    //
    // Code
    //

    /**
     * The user has been registered.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  mixed  $user
     * @return mixed
     */
    protected function registered(Request $request, $user)
    {
        $token = Verification::getVerificationToken($user);
        $url = route('user.verify', $token);
        Mail::to($user->email)->send(new Welcome($url));

        return redirect('/home')->with('success', 'Registered. Verify your email!');
    }

    public function verifyUser($token)
    {
        $user = Verification::verifyUserByToken($token);

        if (auth()->guest())
            auth()->login($user);

        return redirect('/home')->with('success', 'Your email verified successfully!');
    }
}

Here we use the registered method to create token and send it, which will overrides \Illuminate\Foundation\Auth\RegistersUsers@registered method. We get a token for the given user, make it as a url and send it as an email., (*24)

If the user clicks on the link, the verifyUser method will be executed. Here we just verify the user by the given token., (*25)

Please make sure that you add the verifyUser to the except array of the guest middleware in the constructor., (*26)

Relaunch the process

If you want to regenerate and resend the verification token, you can do this with the getVerificationToken method., (*27)

The generate method will generate a new token for the given user and change the verified column to false., (*28)

CONTRIBUTE

Feel free to comment, contribute and help. 1 PR = 1 feature., (*29)

LICENSE

Laravel User Verification is licensed under The MIT License (MIT)., (*30)

The Versions

14/10 2017

dev-master

9999999-dev

A package for user email and phone number verification with activation code

  Sources   Download

MIT

by Mehran Rasulian

laravel code token user verification

14/10 2017

v1.0

1.0.0.0

A package for user email and phone number verification with activation code

  Sources   Download

MIT

by Mehran Rasulian

laravel code token user verification