devhoussam123's avatar

Laravel Breeze redirect to login page after registration?

Laravel Breeze uses the same ##RouteServiceProvider::HOME in two places separately I want to change that behavior in ### RegisteredUserController.php I tried to change return redirect(RouteServiceProvider::HOME); to return redirect('/login'); but did not work?

RegisteredUserController.php

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Providers\RouteServiceProvider;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rules;
use Illuminate\View\View;

class RegisteredUserController extends Controller
{
    /**
     * Display the registration view.
     */
    public function create(): View
    {
        return view('auth.register');
    }

    /**
     * Handle an incoming registration request.
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    public function store(Request $request): RedirectResponse
    {
        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:'.User::class],
            'password' => ['required', 'confirmed', Rules\Password::defaults()],
        ]);

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
        ]);

        event(new Registered($user));

        Auth::login($user);

        return redirect(RouteServiceProvider::HOME);
    }
}
0 likes
2 replies
Snapey's avatar

It can't redirect to login if the user is already logged in.

The RedirectIfAuthenticated middleware will catch it and send the user to the same RouteServiceProvider::HOME

PovilasKorop's avatar
Level 11

@devhoussam123 if you want the user to login manually after registration, you should remove this line in the registration controller:

Auth::login($user);

Then you can redirect to login form, and user won't be auto logged in.

1 like

Please or to participate in this conversation.