Nikki
252
3
Laravel

Trying to get property of a non-object in a middleware

Posted 3 months ago by Nikki

I'm trying to create a middleware that will only allow users that have the user_type customer, but the error I'm getting is

Trying to get property 'user_type' of non-object

I'm not sure why i'm getting this, because I'm assuming that if I go directly to the url that is protected then it should go straight to the home page

here is my middleware

<?php

namespace App\Http\Middleware;

use Closure;
use Auth;

class Customer
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if(Auth::user()->user_type == 'customer'){
            return $next($request);
        }

        return redirect('/')->with('error', 'You have not customer access');
    }
}

and this is my routes

Route::group(['middleware' => 'customer'], function(){
    Route::get('/account/dashboard', '[email protected]')->name('account.dashboard');
    Route::get('/account/details', '[email protected]')->name('account.details');
});

and I have this in my $routeMiddleware

'customer' => \App\Http\Middleware\Customer::class,

Please sign in or create an account to participate in this conversation.