简体   繁体   English

发送邮件验证后,laravel 返回空白注册页面,而不是 email-verify 路由

[英]After sending email verification, laravel returns to blank register page instead of email-verify route

I have created a registration feature in my project where users are sent a link to verify their email once they register an account.我在我的项目中创建了一个注册功能,用户在注册帐户后会收到一个链接以验证他们的电子邮件。

However, after sending this email to users, laravel returns to the a blank page of the register route instead of the verify-email route.但是,在将这封邮件发送给用户之后,laravel 会返回到 register 路由的空白页面,而不是verify-email路由。

Here is my RegisterController file这是我的 RegisterController 文件

    class RegisterController extends Controller
{
    

    use RegistersUsers;

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

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

    public function showRegistrationForm()
    {
        return view('user.auth.register');
    }

    /**
     * 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, [
            'first_name' => ['required', 'string', 'max:255'],
            'last_name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed', 'regex:/^(?=.*[A-Za-z])(?=.*\d)(?=.*[@$!%*#?&])[A-Za-z\d@$!%*#?&]{8,}$/'],
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),

        ]);
    }

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

        event(new Registered($user = $this->create($request->all())));

        // return redirect($this->redirectPath())->with('message', 'Your registration was successful! Login below');
    }
}

Here is my routes\web.php file这是我的routes\web.php文件

Route::get('/', function () {
    return view('user.auth.login');
});

// Routes for only authenticated users

Route::get('/user/verify-email', function () {
    return view('user.auth.verify-email');
})->middleware('auth')->name('verification.notice');


Route::get('/user/verify/{id}/{hash}', function (EmailVerificationRequest $request) {
    $request->fulfill();
    return redirect('/account');
})->middleware(['auth', 'signed'])->name('verification.verify');


Route::post('/user/verify-email/verification-notification', function (Request $request) {
    $request->user()->sendEmailVerificationNotification();

    return back()->with('message', 'Verification link has been re-sent, Please check your email!');
})->middleware(['auth', 'throttle:6,1'])->name('verification.send');


//Route to logout authenticated user

Route::get('/account/logout', [AuthLoginController::class, 'logout'])->name('user.account.logout')->middleware(['auth']);

// Routes for only authenticated & verified users

Route::group(
    [
        'middleware' => ['auth', 'verified'],
    ],
    function () {

        //  Route to main account dashboard of user

        Route::get('/account', function () {
            return view('user.account.index');
        })->name('user.account.index');

        // Route to account profile of user

        Route::get('/account/profile', function () {
            return view('user.account.profile');
        })->name('user.account.profile');
    }
);


// Laravel's route to login page and registration page 
// Automatcially links to custom login page and registration page

Auth::routes();

And here is my verify-email file这是我的verify-email文件

<div class="my-auto">
         <br><br>
         @if (Session::has('message'))
         <small class="alert alert-info">{{ Session::get('message') }}</small>
         <br><br><br>
         @endif
         <h2 style="color:whitesmoke;font-size:30px">Email Verification Sent</h2>
         <br>
         <p>Please click on the link sent to your email, to complete your registration! </p>
         <form action="{{ route('verification.send') }}" method="post">
             @csrf
             <br>
             <button type="submit" style="color:whitesmoke;background:none;padding:15px; border:1px solid whitesmoke">Resend
                 Verification Link <i class="icon ion-md-send" style="padding-left: 5px;"></i></button>
             <a href="{{ route('user.account.logout') }}" style="color:whitesmoke;background:none;padding:15px; border:1px solid whitesmoke">Logout <i class="icon ion-md-exit" style="padding-left: 5px;"></i></a>
         </form>
     </div>

in your ur register在你的注册

protected function create(array $data)
{
    $user =  User::create([
        'first_name' => $data['first_name'],
        'last_name' => $data['last_name'],
        'email' => $data['email'],
        'password' => Hash::make($data['password']),

    ]);


    Auth::login($user);
   return redirect('verify-email');

}

Don't forget to add Auth in user controller不要忘记在用户控制器中添加 Auth

Redirect in register function to verify route在注册函数中重定向以验证路由

public function register(Request $request)
    {
        $this->validator($request->all())->validate();

        event(new Registered($user = $this->create($request->all())));
         Auth::login($user);
         return redirect(route('verification.notice'));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM