How to redirect to admin page with custom admin table

Instead of laravel multi auth, I use my own (primitive) method for admin users.

I just have an extra table in the database called PAdmin , and in this table I have the admin user id.

When I enter the application, I want to redirect admin users to another page, and not to the home page.

For this reason, I went to Http \ Middleware \ RedirectIfAuthenticated and changed the code like 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 a user ID in the table PAdmin $isAdmin will be false and elseif will be executed.

Unfortunately, this does not work, and I do not know why.

Perhaps this is the wrong way to do it at all.

Can someone help me get this right.

PS I do not want to use multi auth .

+5
source share
1 answer
  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 light.

+3
source

Source: https://habr.com/ru/post/1274371/


All Articles