1

I've create a new middleware name Adminpanel

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class Adminpanel
{
    public function handle($request, Closure $next)
    {
        if(Auth::user()->role == 'admin'){
            return $next($request);
        }else{
            return redirect('admin/login');
        }
    }
}

and register middle ware

protected $routeMiddleware = [
    'auth' => \App\Http\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'adminpanel' => \App\Http\Middleware\Adminpanel::class,
];

and route

Route::group(['middleware' => ['web','adminpanel']], function () {
Route::get('/admin/dashboard/', 'admin\Dashboard@index');
//

});

but when I run, It ask me Trying to get property of non-object, means I could not access Auth class here, can somebody tell me what is the mistake and how to access Auth Facade in middleware.

and this is my authentication code

public function authenticate(Request $request)
{
    if (Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password'), 'role' => 'admin'], $request->input('remember'))) 
    {
        return redirect()->intended('admin/dashboard');
    }
    else{
        return redirect('admin')->with('response',-1);
    }
}

Thanks

jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
Shoaib Rehan
  • 526
  • 6
  • 16

1 Answers1

3

It doesn't mean you couldn't access Auth class. It means, that user is not authenticated and Auth::user() returns NULL.

Make sure that only authenticated users can access your route by using auth middleware for that route or first check if user is authenticated:

if(Auth::check() && Auth::user()->role == 'admin'){
  return $next($request);
} else {
  return redirect('admin/login');
}
jedrzej.kurylo
  • 39,591
  • 9
  • 98
  • 107
  • What does it change? Is this the part of your code that is causing the error? – jedrzej.kurylo Jan 15 '16 at 07:22
  • No, the authenticate function successfully authenticate user and redirect to dashboard page but auth in middleware is working as I mention above. – Shoaib Rehan Jan 15 '16 at 07:26
  • The fix above should fix the error, but if your authentication doesn't work properly it will redirect you back to login page - could you confirm that it works like that with above code? If so, we'll get to fixing your authentication – jedrzej.kurylo Jan 15 '16 at 07:29
  • What is the primary key for your user model? – jedrzej.kurylo Jan 15 '16 at 07:31
  • 1
    you are right, authentication is not working, my users table primary key is id and manual authenticate method is describe above. ref: https://laravel.com/docs/5.2/authentication – Shoaib Rehan Jan 15 '16 at 08:43
  • I've done, actually my CSRF Token was not working. Thanks – Shoaib Rehan Jan 15 '16 at 09:24