Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

riok09's avatar

Multi-Auth in laravel 6 (Login using multiple table but on single login page)

I have seen many posts here about this multi-auth problem of mine but none of them are helping me.

So here is my code,

<?php

namespace App\Traits\Auth;

use App\Http\Requests\LoginRequest;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Http\Request as HttpRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\ValidationException;

trait LoginTraits {
    use ThrottlesLogins;

    public function login(LoginRequest $request)
    {
        // If the class is using the ThrottlesLogins trait, we can automatically throttle
        // the login attempts for this application. We'll key this by the username and
        // the IP address of the client making these requests into this application.
        if (method_exists($this, 'hasTooManyLoginAttempts') &&
            $this->hasTooManyLoginAttempts($request)) {
            $this->fireLockoutEvent($request);

            return $this->sendLockoutResponse($request);
        }
        //dd(Auth::guard()->attempt($this->credentials($request), $request->filled('remember')) == true);
        if(Auth::guard()->attempt($this->studentcredentials($request), $request->filled('remember')) == true)
        {
            $request->session()->regenerate();
            $this->clearLoginAttempts($request);
            return redirect()->intended('dashboard/student');

        }else{
            $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
        }

        if(Auth::guard('staff')->attempt($this->credentials($request), $request->filled('remember')) == true)
        {
            $request->session()->regenerate();
            $this->clearLoginAttempts($request);
            return redirect()->intended('dashboard/staff');
        }else{
            $this->incrementLoginAttempts($request);

        return $this->sendFailedLoginResponse($request);
        }
        /* if(Auth::guard('admin')->attempt($this->credentials($request), $request->filled('remember')))
        {
            $request->session()->regenerate();

            $this->clearLoginAttempts($request);

            return redirect()->intended('dashboard/admin');
        } */
    }

    public function studentcredentials(LoginRequest $request)
    {
       return $request->only($this->username() , 'password');

       /* if(is_numeric($request->only('email'))){
        return $request->only('mobile', 'password');
      }
      elseif (filter_var($request->only('email'), FILTER_VALIDATE_EMAIL)) {
        return $request->only('email', 'password');
      }
      return $request->only('assign_id', 'password'); */
    }

    public function credentials(LoginRequest $request)
    {
        return $request->only($this->username() , 'password');
    }

    /**
     * Get the failed login response instance.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Symfony\Component\HttpFoundation\Response
     *
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function sendFailedLoginResponse(LoginRequest $request)
    {
        throw ValidationException::withMessages([
            $this->username() => [trans('auth.failed')],
        ]);
    }

    public function logout(HttpRequest $request)
    {
        $this->guard()->logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();
        return redirect('/');
    }

    public function username()
    {
        return 'email';
    }

    /**
     * Get the guard to be used during authentication.
     *
     * @return \Illuminate\Contracts\Auth\StatefulGuard
     */
    public function guard()
    {

        return Auth::guard();
    }
}

The code above is the login controller

So mainly there is a login page from which I want to authenticate students and staff with there an email or mobile or unique_id. there are two different tables both have there passwords and emails and every detail.

I have created the guards in the auth.php page.

The problem is that it only goes to one guard which the default one and I can't log in using other credentials

0 likes
2 replies
bobbybouwmann's avatar

Well, the problem here is that you check for the student one. But if it fails you return the sendFailedLoginResponse. Because of that you never get to the staff auth

Try this instead

if (Auth::guard()->attempt($this->studentcredentials($request), $request->filled('remember')) == true) {
    $request->session()->regenerate();
    $this->clearLoginAttempts($request);

    return redirect()->intended('dashboard/student');
} else if (Auth::guard('staff')->attempt($this->credentials($request), $request->filled('remember')) == true) {
    $request->session()->regenerate();
    $this->clearLoginAttempts($request);

    return redirect()->intended('dashboard/staff');
} else {
    $this->incrementLoginAttempts($request);

    return $this->sendFailedLoginResponse($request);
}

Do you see the difference here?

riok09's avatar

Hey, so I applied the exact code you gave me but it is not working...

now if I log in with the student cred. I am redirected to the staff dashboard page and if I log in with student cred. it does nothing i am not even getting the validation request or anything its just stays on the login page.

Please or to participate in this conversation.