Working with Laravel 10, Windows 10, Wamp.

I am trying to redirect users that have a specific permission to a different page than the home page.

Or, if they tried to login from the shop page, then redirect them back to the shop’s page.

I get a very strange behavior with this.

Trying to login, I get this error:

SymfonyComponentHttpFoundationResponse::setContent(): Argument #1 ($content) must be of type ?string, IlluminateRoutingRedirector given, called in C:wamp64wwwhighland10.localvendorlaravelframeworksrcIlluminateHttpResponse.php on line 72

I change the code in the LoginController to something else, I commit, it works fine, then the day after, or moving to a different computer, I get the error.

I change the code again, works fine, the day after or a different computer, the same error.

And so on.

I don’t understand what is going on.

In my routes file I have this:

Route::group(['middleware' => 'auth', 'can:admin dashboard'], function () {
    Route::get('/bean', [AdminController::class, 'index'])->name('admin');
});

The codes I tried, in LoginController:

protected function authenticated($request, $user)
{        
    $previousUrl = Session::get('previousUrl', '/');
    
    $url = session()->get('url.intended');
    
    if($previousUrl != '/') {
        Session::forget('previousUrl');            
        return redirect($previousUrl);
    } else { 
        if (auth()->user()->can('admin dashboard'))
            return redirect('/bean'); // the problematic code
    }
    
    return redirect($this->redirectTo);
}

Worked fine, then the day after, got the error.

Changed the code, instead of:

return redirect('/bean');

Changed to:
    return to_route('admin');    

Worked fine until the day after.

Sometimes it works fine for days, and then out of the blue, gives the error.

Can anyone please help?