I have a problem dealing with middleware,
here is AdminMiddleware code:
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check() && Auth::user()->admin == 1) {
return $next($request);
}else{
return redirect('/admin-login');
}
}
Here is route code:
Route::group(['middleware' => 'admin'], function () {
Route::get('/dashboard', 'BackEnd\HomeController@getDashboard');
});
But when i login then it doesn't work and redirect to only login page, please guide me how to deal with middleware issue.
Your password is not in bycryted you should do
echo bycryp(12345) // paste this in db and then try again
I hope this this works not tested
public function handle($request, Closure $next, $guard = null)
{
if (Auth::check() && Auth::user()->admin == 1) {
return $next($request);
} else {
return redirect('/admin-login');
}
}
You want a group of admin-only routes, right? You could instead use the can
middleware
Route::group(['middleware' => 'can:admin'], function () {
Route::get('/dashboard', 'BackEnd\HomeController@getDashboard');
});
which will check the authorization policy, which you can define in your Providers/AuthServiceProvider.php
:
Gate::define('admin', function (User $user) {
return $user->admin == 1;
});
If the check fails, you will get an "Unauthorized" response.
If instead you want the redirect to the login page, you could insist on your custom middleware, it should just be
if (Auth::check() && Auth::user()->admin == 1)
Adnan Mumtaz's answer is correct, the password should not be in plain text in the database, instead it should be encrypted (save instead the result of bcrypt('12345')
).