Skip to content
Advertisement

My guard always returs null in custom middleware when trying to login

I am trying to make multi auth but i just cannot seem to make it work. I am using custom middleware instead of RedirectIfAuthenticated middleware but guard always returns null in my middleware but when i use RedirectIfAuthenticated middleware then it works. here is my code

Auth.php

   'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

/*
|--------------------------------------------------------------------------
| Authentication Guards
|--------------------------------------------------------------------------
|
| Next, you may define every authentication guard for your application.
| Of course, a great default configuration has been defined for you
| here which uses session storage and the Eloquent user provider.
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| Supported: "session", "token"
|
*/

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
        'hash' => false,
    ],

    'admin' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],

    'user' => [
        'driver' => 'session',
        'provider' => 'users'
    ],  
],

/*
|--------------------------------------------------------------------------
| User Providers
|--------------------------------------------------------------------------
|
| All authentication drivers have a user provider. This defines how the
| users are actually retrieved out of your database or other storage
| mechanisms used by this application to persist your user's data.
|
| If you have multiple user tables or models you may configure multiple
| sources which represent each model / table. These sources may then
| be assigned to any extra authentication guards you have defined.
|
| Supported: "database", "eloquent"
|
*/

'providers' => [
    'admins' => [
        'driver' => 'eloquent',
        'model' => AppAdmin::class,
    ],

    'users' => [
        'driver' => 'eloquent',
        'model' => AppUser::class,
    ],

    // 'users' => [
    //     'driver' => 'database',
    //     'table' => 'users',
    // ],
],

user model

    public $table = 'users';
use Notifiable;

/**
 * The attributes that are mass assignable.
 *
 * @var array
 */

 protected $guard = 'user';

protected $fillable = [
    'name', 'email', 'password',
];

/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];

/**
 * The attributes that should be cast to native types.
 *
 * @var array
 */
protected $casts = [
    'email_verified_at' => 'datetime',
];

public function verifyUser() {
    return $this->hasOne('AppVerifyUser');
}

}

userLoginController

    use AuthenticatesUsers;

public function __construct()
{
    $this->middleware('guest:user')->except('logout');
}

public function showLoginForm() {
    return view('user.login');
}

protected function guard() {
    return Auth::guard('user');
}

public function login(Request $request) {
    $this->validate($request, [
        'email' => 'required|email',
        'password' => 'required|min:8'
    ]);

    if(Auth::guard('user')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
        // return $this->sendLoginResponse($request);
        // if(Auth::check()) {
            return redirect()->route('userdashboard');
        // }
    }

    return redirect()->back()->withInput($request->only('email', 'remember'));
}

protected function authenticated(Request $request, $user) {   
    echo $user->email_verified_at;
    die();
    // if($user->email_verified_at == null) {
    //     Auth::logout();
    //     return back()->with('warning', 'You need to confirm your email account. We have sent you an email verification code. Please check your email.');
    // }
}

public function logout() {
    session::flush();
    Auth::logout();
    return redirect()->route('userlogin');
}

protected function loggedOut(Request $request)
{
    return redirect('user/login');
}

}

And this is my middleware

    public function __construct()
{
    // Auth::shouldUse('user');
}
/**
 * Handle an incoming request.
 *
 * @param  IlluminateHttpRequest  $request
 * @param  Closure  $next
 * @return mixed
 */
public function handle($request, Closure $next, $guard = null)
{
    if($guard == 'user' && Auth::guard($guard)->check()){
        return $next($request);
    } else {
        return redirect('home')->with('error', 'You do not hava user access');
    }

    return $next($request);
}

Advertisement

Answer

If you want your Middleware to get a parameter you have to define it with the parameter when you are assigning the middleware:

Route::get(...)->middleware('user:user');

The first user is the middleware’s name. After the : are the parameters that will be passed to your middleware’s handle method. So this middleware would end up with $guard being user.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement