2

I am building my first Laravel app with the Metronic 8 Laravel theme. It uses Breeze for authentication. I changed a couple of things around - created a welcome page for non-logged-in users, and moved the main template that was the index to an auth protected "/dashboard". The problem is that it still tries to load the dashboard Blade template, regardless of authentication, resulting in an error.

Route

Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth'])->name('dashboard');

Here's Authenticate, where it should redirect non-authenticated users to the login page.

protected function redirectTo($request)
{
    if (! $request->expectsJson()) {
        return route('login');
    }
}

When I'm not logged in and navigate to the dashboard URL, it attempts to load the dashboard Blade template, which calls a menu function that checks the user permissions for menu items. Unfortunately, since there is no user, the application blows up from passing a null value to a method expecting a user array/object.

Any ideas on where to look for the problem? It seems to me that the auth middleware should redirect to the login page before trying to load the Blade template when not logged in.

MichalOravec
  • 1,560
  • 3
  • 5
  • 20
scott80109
  • 361
  • 6
  • 25

3 Answers3

1

I would put the middleware at the beginning of the route like this, though I'm sure it's not causing the problem-

Route::middleware(['auth'])->get('/dashboard', function () {
    return view('dashboard');
})->name('dashboard');

Aside from that, please provide some information on the error itself like what the error is about/what is says..etc...

Wakil Ahmed
  • 1,053
  • 1
  • 8
  • 16
1

First of all, make sure you have a login named route defined in your routes/web.php file. It should look something like:

Route::get('/login', '<controller>@<method>')->name('login');

The important bit is ->name('login') so that the Authenticate middleware can correctly identify the route to redirect to. Change <controller>@<method> appropriately to route to the login method of your app.

Wakil's answer is irrelevant and actually opposite of the documentation. Your syntax is correct.

citysurrounded
  • 664
  • 4
  • 8
0

I figured out the issue. Keen Themes put a call to a method to build an array of menu items in the web routes file. That was making the call to the offending code. After I wrapped that in an auth check the error was fixed, and everything works as expected.

scott80109
  • 361
  • 6
  • 25