在RegistrationController中创建用户之前进行验证

时间:2019-03-08 12:38:29

标签: php laravel laravel-5

在此Laravel脚本中,当用户输入他的详细信息进行注册时,Laravel首先创建用户,然后发送电子邮件进行验证,相反,我想要此操作: 我想要在用户输入详细信息之后,Laravel发送电子邮件验证,如果验证成功,则创建用户。

RegistrationController:

<?php

namespace App\Http\Controllers\Auth;

use App\GeneralSetting;
use App\Service;
use App\ServicePrice;
use App\User;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default, this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/

use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = 'user/dashboard';

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

    /**
     * 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|string|max:255',
            'email' => 'required|string|email|max:255|unique:users',
            'password' => 'required|string|min:6|confirmed',
            'username' => 'required|string|alpha_dash|max:25|unique:users',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        $general = GeneralSetting::first();
        $code = str_random(6);
        if($general->email_verification == 1){
            $ev = 0;
            send_email($data['email'], $data['name'], 'Verification'
            ,'Your code is'.':' . $code);
        }else {
            $ev = 1;
        }
        $api = str_random(30);
         $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'username' => $data['username'],
            'password' => bcrypt($data['password']),
            'verification_time' => Carbon::now(),
            'verification_code' => $code,
            'email_verify' => $ev,
            'api_key' => $api,
        ]);
        $services = Service::all();
        foreach ($services as $service){
            $servicePrice = new ServicePrice();
            $servicePrice->category_id = $service->category_id;
            $servicePrice->service_id = $service->id;
            $servicePrice->user_id = $user->id;
            $servicePrice->price = $service->price_per_k;
            $servicePrice->save();
        }
        return $user;

    }
}

2 个答案:

答案 0 :(得分:0)

用户注册时,他提供的信息至少存储在users表中,您将模型的$table属性设置为其他属性。将用户信息保存在表中的事实是用户注册过程的一部分。奇怪的是,您只想在用户验证其电子邮件地址后才注册该用户。我的建议是,即使用户尝试登录,也不要在用户注册后将其重定向到另一个页面,您将登录条件设置为仅记录已验证其电子邮件地址的用户。

答案 1 :(得分:0)

您应该实现与密码重置工作原理类似的功能。

您可以保持创建方法不变。您的RegisterController内部还有一个register()函数。

1。。在该功能中,您应该覆盖用户登录的部分,而应将其重定向到带有消息的页面,该消息说已发送电子邮件,并且他需要验证它。

现在我看到您通过电子邮件发送代码

2。。您还应该在电子邮件中提供一个链接,以将用户重定向到代码提交页面。

3。。如果没有这样的页面,则应创建一个页面。刀片文件,查看它的功能以及访问它的web.php文件上的路由。

4。。在该页面内,您将有一个<form>和一个<input>字段,例如“代码”,其动作将指向您要创建的功能,例如RegisterController中的validateCode()。

然后,此功能工作将检查用户表中是否有与请求中提供的代码相同的代码的用户,如果该用户存在,则它将“ email_verify”字段更新为该用户的1登录并将其重定向到面板,如果不是,它将重定向回代码提交视图:

public function validateCode(Request $request)
{
    $user = User::whereVerificationCode($request->get('code'))->first();
    if($user){
        $user->verify_email = true;
        $user->update();
        Auth::login($user);
        return redirect()->route('home');
    }else{
        return redirect()->back();
    }    
}

出于安全原因,如果将要生成的代码更改为9或10位数字,甚至更好的方式更改为哈希字符串,也将是很好的选择。