i have two custom guards "customer" and "adminuser" with separate controller files. default loginController for customers and AdminController for admin users. For admin user i have CheckRole middleware and guest middleware for customer. when i login as a admin user it will always login as same admin user. different admin login details are not working. when i click logout, it will logout and next time i login it shows this error "Trying to get property 'id' of non-object". I'm using Laravel 5.8 , i stucked in this case for a week. why this login not work? where is the problem. i am new to laravel. please help me. Thanks in advance.
Here is my admin login form
@if ($errors->any())
@foreach ($errors->all() as $error)
<div class="alert alert-danger" role="alert">
<strong>Oops!</strong>
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
@endforeach
@endif
<form action="/adminUserLogin" method="POST">
@csrf
<div class="input-group mb-3">
<input type="text" class="form-control" placeholder="Username" name="username" id="username">
<div class="input-group-append">
<span class="fa fa-user input-group-text"></span>
</div>
</div>
<div class="input-group mb-3">
<input type="password" class="form-control" placeholder="Password" name="password" id="password">
<div class="input-group-append">
<span class="fa fa-lock input-group-text"></span>
</div>
</div>
<div class="row">
<div class="col-8">
<div class="checkbox icheck">
<label>
<input type="checkbox"> Remember Me
</label>
</div>
</div>
<!-- /.col -->
<div class="col-4">
<button type="submit" class="btn btn-primary btn-block">Sign In</button>
</div>
<!-- /.col -->
</div>
</form>
LoginController.php for customer login
<?php
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Auth;
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest:customer')->except('customerlogout');
}
//login for the customer
public function customerLogin(Request $request)
{
$this->validate($request, [
'username' => 'required|min:3',
'password' => 'required|min:6|max:6'
]);
$status = Auth::guard('customer')->attempt([
'email' => $request->username,
'password' => $request->password],
$request->get('remember'));
if ($status) {
return redirect()->intended('/customerhome');
}
return redirect()->intended('/customerhome');
}
public function customerlogout(Request $request)
{
Auth::guard('customer')->logout();
return redirect('/index');
}
}
AdminController.php for admin user login
<?php
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Auth;
class AdminController extends Controller
{
use AuthenticatesUsers;
public function __construct()
{
$this->middleware('roles:adminuser')->except('adminlogout');
}
public function AdminLogin(Request $request)
{
$this->validate($request, [
'username' => 'required|min:3',
'password' => 'required|min:6|max:6'
]);
$status = Auth::guard('adminuser')->attempt([
'email' => $request->username,
'password' => $request->password,
]);
if ($status) {
return redirect()->intended('/adminhome');
}
return redirect()->back();
}
public function adminlogout(Request $request)
{
Auth::guard('adminuser')->logout();
$request->session()->flush();
return redirect('/admin/login');
}
}
CheckRole Middleware
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
use App\Models\AdminUser;
class CheckRole
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if ($guard === "adminuser" && Auth::guard($guard)->check()) {
return redirect('/adminhome');
}
if (Auth::guard($guard)->check()) {
return redirect('/admin/login');
}
return $next($request);
}
}
AdminUser Model
public function hasRole()
{
if(Auth::guard('adminuser')->check()){
$admin_user_id = Auth::guard('adminuser')->user()->id;
$role_id = $this->roles()->select('role_name')->where('roles.id', $admin_user_id)->first();
return $role_id;
}
}
Please help me! Thanks in Advance.
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2lIBZmd
via IFTTT
Aucun commentaire:
Enregistrer un commentaire