简体   繁体   中英

How to redirect to admin page with custom admin table

Instead of the laravel multi auth I am using my own (primitive) method to have admin users.

I just have an extra table in the database called PAdmin and in this table I have the id's of the admin users.

When I log in to the application I want to redirect the admin users to another page instead of the home page.

For that reason, I went to Http\\Middleware\\RedirectIfAuthenticated and changed the code so from this :

 public function handle($request, Closure $next, $guard = null)
{

  if (Auth::guard($guard)->check()) {
  return redirect('/home');

 }

 return $next($request);


}

I changed it to this :

 public function handle($request, Closure $next, $guard = null)
{

    $user_id = Auth::id();


    $isAdmin = PAdmin::where('user_id',$user_id)->get()->isEmpty();//returns 'true' if empty

    if (Auth::guard($guard)->check() && $isAdmin) {//IF $isAdmin is TRUE it means that the user is not admin

        return redirect('/home');
    }elseif (Auth::guard($guard)->check() && !($isAdmin)){//IF $isAdmin is FALSE it means that the user is not admin

        return redirect('/admin');
    }

    return $next($request);
}

The idea is simple - if there is a record with the user's ID in the PAdmin table $isAdmin will be false and the elseif will execute.

Unfortunately this isn't working and I don't know why.

Maybe this isn't the right way to do it at all.

Can someone help me get this right.

PS I don't want to use multi auth .

 public function handle($request, Closure $next, $guard = null)
{

  if (Auth::guard($guard)->check()) {
     if(PAdmin::where('user_id',Auth::id())->first() != null) return redirect('/admin');
     return redirect('/home');

 }

 return $next($request);


}

More clear and easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM