rasulian/laravel-user-verification 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

rasulian/laravel-user-verification

Composer 安装命令:

composer require rasulian/laravel-user-verification

包简介

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

README 文档

README

A simple package to activate the users by token and code number.

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

Installation

This package can be used in Laravel 5.4 or higher.

You can install the package via composer:

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:

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

You may add the following aliases to your config/app.php:

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

Publish the package config and database migration file by running the following command:

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.

Please make sure that you don't have the this column on your user table.

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

Now you can migrate the normal way you do:

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.

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

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:

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.

  • 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.

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.

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

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

Then, add your custom route in your route file.

Facade

The package offers a facade Verification::.

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.

<?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.

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

Edit the App\Http\Auth\RegisterController file:

<?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.

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

Please make sure that you add the verifyUser to the except array of the guest middleware in the constructor.

Relaunch the process

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

The generate method will generate a new token for the given user and change the verified column to false.

CONTRIBUTE

Feel free to comment, contribute and help. 1 PR = 1 feature.

LICENSE

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

rasulian/laravel-user-verification 适用场景与选型建议

rasulian/laravel-user-verification 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 39 次下载、GitHub Stars 达 6, 最近一次更新时间为 2017 年 10 月 14 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「code」 「laravel」 「token」 「user verification」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 rasulian/laravel-user-verification 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 rasulian/laravel-user-verification 我们能提供哪些服务?
定制开发 / 二次开发

基于 rasulian/laravel-user-verification 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 39
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 9
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 6
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-10-14