Framework: RedirectIfAuthenticated Middleware always pass $guard param null to handle function

Created on 12 Nov 2018  路  8Comments  路  Source: laravel/framework

  • Laravel Version: 5.7.13
  • PHP Version: 7.1.15
  • Database Driver & Version: Mysql

Description:

at AppHttp\MiddlewareRedirectIfAuthenticated;

$guard param always passed null to handle function when using a special guard as it for my case :

public function __construct(Request $request)
{
    $this->request = $request;
    $this->middleware("auth:members");
}
needs more info

Most helpful comment

Your middlewares seems mis-configured.
I am using this package to generate scaffolding for multi-auth.

https://github.com/mtvbrianking/multi-auth
I never faced any issue.

All 8 comments

Can you post the full class with the constructor above? Where is this done? In a middleware? A controller?

RedirectIfAuthenticated is used when you're authed but you want the routes/views be accessible as a guest. So placing the auth middleware doesn't really makes sense. Are you sure you're not mistaking this with the Authenticate middleware?

yes sure, actually I do have different landing dashboard related to different auth guards and this is my class :

class MemberController extends Controller
  {

        public function __construct(Request $request)
        {
            $this->request = $request;
            $this->middleware("auth:webMembers");
        }


    ... resources methods 
   }

and its shuld redirect to the correct login page based on auth guard that had made the authentication

this is how i did fixed it

namespace App\Http\Middleware;

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

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {

        if ($request->segment(1) == "member") {
            $guard = "webMembers";
        }

        if (Auth::guard($guard)->check()) {
            return redirect('/'. $request->segment(1));
        }

        return $next($request);
    }
}

I don't see any issue. In my case the guard value is what i expected.

app/Http/Controllers/Admin/Auth/LoginController.php

<?php

namespace App\Http\Controllers\Admin\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/admin';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest:admin', ['except' => 'logout']);
    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    protected function guard()
    {
        return Auth::guard('admin');
    }

    /**
     * Show the application's login form.
     *
     * @return \Illuminate\Http\Response
     */
    public function showLoginForm()
    {
        return view('admin.auth.login');
    }

    /**
     * The user has logged out of the application.
     *
     * @param  \Illuminate\Http\Request $request
     * @return mixed
     */
    protected function loggedOut(Request $request)
    {
        return redirect()->route('admin.login');
    }

    /**
     * Where to redirect users after login.
     *
     * @return string
     */
    private function redirectTo()
    {
        return route('admin.dashboard');
    }

}

app/Http/Middleware/RedirectIfAuthenticated.php

<?php

namespace App\Http\Middleware;

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

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request $request
     * @param  \Closure $next
     * @param  string|null $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = 'web')
    {
        if (Auth::guard($guard)->check()) {
            if ($guard === 'admin') {
                return redirect()->route('admin.dashboard');
            }
            return redirect()->route('dashboard');
        }

        return $next($request);
    }
}

You can treat null as web the default guard.

Ok I just caught specific scenario producing this issue is when I logged in with the default web guard
and at the same time in another browser Tab when I do logging in with member guard the guard passed null and I get this error

image

Your middlewares seems mis-configured.
I am using this package to generate scaffolding for multi-auth.

https://github.com/mtvbrianking/multi-auth
I never faced any issue.

Great package it really saved my time its working fine now, thanks.

I am having the same problem. RedirectIfAuthenticated middleware always return false on Auth::guard($guard)->check() while $guard is 'admin'

Was this page helpful?
0 / 5 - 0 ratings