简体   繁体   中英

Laravel 5.3 Redirect User after Registration

I'm using Laravel 5.3 and the php artisan command make:auth to scaffold the registration and login. After a user registers, I want to redirect him to a page that says something like "Verify your email" and I don't want it to automatically login the user like default.

I can only think of, in the create() method in the RegisterController, instead of returning the User(I assume that's where it automatically logins), I want to redirect to another view.

protected function create(array $data)
{
    $confirmation_code = str_random(30);

    Mail::to($data['email'])->send(new Company($confirmation_code));

    User::create([
        'confirmation_code' => $confirmation_code,
        'password' => bcrypt($data['password']),
        'email' => $data['email']
    ]);

    return redirect()->route('verifyemail');
}

But I get this error: Argument 1 passed to Illuminate\\Auth\\SessionGuard::login() must implement interface Illuminate\\Contracts\\Auth\\Authenticatable, instance of Illuminate\\Http\\RedirectResponse given, called in C:\\xampp\\htdocs\\app\\vendor\\laravel\\framework\\src\\Illuminate\\Foundation\\Auth\\RegistersUsers.php on line 32 and defined

I tried to override the register(Request $request) method in the RegistersUsers.php to take out the line that makes the logins, but it's still not working.
Any ideas?

EDIT:I added $this->guard()->logout(); to the overrided register method after it does the login. It works, but it's not the correct way to do this and I would like to find another solution..

I would create your own register controller for more flexibility. And in their make the create/store method that creates and then redirects to the place you want. Wouldn't take too long

in app/Http/Controllers/Auth/RegisterController add this after Method create:

enter code here

public function redirectPatch() 
{
  return "verifyemail";
} 

Exemple

protected function create(array $data)
{
    $confirmation_code = str_random(30);

    Mail::to($data['email'])->send(new Company($confirmation_code));

    User::create([
        'confirmation_code' => $confirmation_code,
        'password' => bcrypt($data['password']),
        'email' => $data['email']
    ]);

}
public function redirectPatch() 
{
  return "/verifyemail";
} 

For weeks I have been trying to find a solution for this. To override the default URL after registering, just add inside the create function the following:

$this->redirectTo = '/url-after-register';

Just like this

protected function create(array $data) {
    $this->redirectTo = '/url-after-register';

    return User::create([...]);
}

Thanks! I have implemented your solution. Or you can override registered method in your class and redirect to your specific route.

protected function registered(Request $request, $user)
{
    $this->guard()->logout();
    return Redirect::route('your.route');
}

Use these methods in your RegisterController.php

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

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

    Mail::to($user->email)->send(new ConfirmationEmail($user));

    return back()->with('status', 'Thanks for signing up! Please check your email.');
}

public function confirmEmail($confirmation_code)
{
  User::whereConfirmationCode($confirmation_code)->firstOrFail()->hasVerified();
  return redirect('login')->with('status', 'You have successfully verified your account. Please Login.');
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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