如何使用laravel inbuild auth模块进行电子邮件验证

时间:2016-08-23 07:48:36

标签: php laravel authentication laravel-5.2 laravel-authorization

我通过php artisan make:auth

创建了auth模块

我尝试在电子邮件验证后转换注册成功,但发现所有身份验证代码都在供应商文件夹中。

以下路线

 // Authentication Routes...
    Route::get('login', 'Auth\AuthController@showLoginForm');
    Route::post('login', 'Auth\AuthController@login');
    Route::get('logout', 'Auth\AuthController@logout');

    // Registration Routes...
    Route::get('register', 'Auth\AuthController@showRegistrationForm');
    Route::post('register', 'Auth\AuthController@register');

    // Password Reset Routes...
    Route::get('password/reset/{token?}', 'Auth\PasswordController@showResetForm');
    Route::post('password/email', 'Auth\PasswordController@sendResetLinkEmail');
    Route::post('password/reset', 'Auth\PasswordController@reset');

直接创建用户和登录。但我想通过发送带有令牌的电子邮件来验证用户的电子邮件。

我知道供应商的更新不是一个好主意,请建议我处理这个,或者我应该创建自己的auth模块?

谢谢你的时间, 欢呼声。

1 个答案:

答案 0 :(得分:0)

注册过程通过特征中的方法register

 /**
 * Handle a registration request for the application.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function register(Request $request)
{
    $validator = $this->validator($request->all());

    if ($validator->fails()) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    Auth::guard($this->getGuard())->login($this->create($request->all()));

    return redirect($this->redirectPath());
}

然后这个方法在app目录的AuthController中调用两个函数,你可以根据需要进行编辑:

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|confirmed|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return User
     */
    protected function create(array $data)
    {
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            //'token' => str_random(10)
        ]);

        //Send email to $user here
        return $user

    }
}

为了防止用户登录,请更改Auth Middleware:

class Authenticate
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        if (Auth::guard($guard)->guest()) {
            if ($request->ajax()) {
                return response('Unauthorized.', 401);
            } else {
                return redirect()->guest('login');
            }
        }

        if (!Auth::user()->validated())
        {
            return redirect()->route('error.message.route');
        }
        return $next($request);
    }
}