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

Mona_Salih12's avatar

how to use second middleware

When attempting to log in through the new login page, it should connect to the new middleware named 'EmployerMiddleware.' However, it continues to display the message 'These credentials do not match our records' on the login form. Upon investigation, I discovered that instead of checking the credentials in the 'employer_information' table, it persists in checking if the email and password exist in the default 'user' table. How can I correct this within the following code? EmployeerMiddleware.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use App\Models\EmployerInformation ;
use Illuminate\Support\Facades\Auth;

class EmployeerMiddleware
{
 
    public function handle(Request $request, Closure $next)
    {
       
        if ($request->has('email') && $request->has('password')) {
            $credentials = $request->only('email', 'password');
            if (Auth::guard('employers')->attempt($credentials)) {
                // Authentication passed...
                return $next($request);
            } else {
                // Authentication failed...
                dd('Authentication failed'); // Debugging statement
            }
        

        }
        return redirect()->route('empLogin')->withErrors(['email' => 'Invalid credentials']);
    }
}

kernal.php

'EmployeerMiddleware'=>\App\Http\Middleware\EmployeerMiddleware::class,

config\auth.php

   'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'employers' => [
            'driver' => 'session',
            'provider' => 'employers',
        ],
    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
        'employers' => [
            'driver' => 'eloquent',
            'model' => App\Models\EmployerInformation::class,
        ],

Model\EmployerInformation.php

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class EmployerInformation extends Model implements Authenticatable
{
    protected $table = 'employer_information';
    use HasFactory;

    // Implement the required methods from the Authenticatable interface
    public function getAuthIdentifierName()
    {
        return 'id'; // Assuming 'id' is the primary key of your employer_information table
    }

    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    public function getAuthPassword()
    {
        return $this->password;
    }

    public function getRememberToken()
    {
        return $this->remember_token;
    }

    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    public function getRememberTokenName()
    {
        return 'remember_token';
    }
}

web.php

Route::get('/employer/login', function () {
  return view('auth.employer_auth.login_employeer');
})->name('empLogin');

//employer route
Route::prefix('EmployerLog')->middleware(['auth','EmployeerMiddleware'])->group(function(){
  Route::get('employer/showEmployer', function () {
      return view('employer.showEmployer');
  })->name('employer.show');
  // Other routes here
});

0 likes
13 replies
gych's avatar

Its better to use a single table for users and work with roles, will save you a lot of issues. In the end an employer is also a user in the application so why add employer user accounts in a separate table?

1 like
Mona_Salih12's avatar

@gych I want to create a separate login page for employers. So, I've set up separate middleware, routes, table and guards for this purpose

1 like
Snapey's avatar

as before, you will get nowhere using a middleware for this.

Also, as you have been advised, everyone is a user and should be authenticated as a user

Snapey's avatar

@Mona_Salih12 but why? It makes no sense?

Also, your login should be validated by a controller, and then a middleware could be used to protect routes that are only available to employer.

Mona_Salih12's avatar

@Snapey

To clarify, both myself and the team are working on the project. My specific responsibility is to focus on the middleware and guard implementation.

Mona_Salih12's avatar

Hello everyone I have been solve it with my college like following EmployerAuthController.php

 public function login(Request $request)
    {

        if ($request->has('email') && $request->has('password')) {
            $credentials = $request->only('email', 'password');

            if (Auth::guard('employer')->attempt($credentials)) {
                return view('employer.showEmployer');
            }
        }
        return redirect()->route('empLogin')->withErrors(['email' => 'Invalid credentials']);
    }


    public function emplogout (Request $request){
        Auth::guard('employer')->logout();
        return redirect()->route('empLogin');
    }

EmployeerMiddleware.php

public function handle(Request $request, Closure $next)
    {
        if ($request->has('email') && $request->has('password')) {
            $credentials = $request->only('email', 'password');
            if (Auth::guard('employer')->attempt($credentials)) {
                return $next($request);
            }
        }
        return redirect()->route('empLogin')->withErrors(['email' => 'Invalid credentials']);
    }

kernal.php

 'EmployeerMiddleware'=>\App\Http\Middleware\EmployeerMiddleware::class,

EmployerModel.php

class Employer extends Model implements Authenticatable
{
    protected $table = 'employers';
    use HasFactory;

    // Implement the required methods from the Authenticatable interface
    public function getAuthIdentifierName()
    {
        return 'id'; // Assuming 'id' is the primary key of your employer_information table
    }

    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

 ///complete other function
}

config\auth.php

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

    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
        
        'employer' => [
            'driver' => 'eloquent',
            'model' => App\Models\Employer::class,
        ],

web.php

///must write the following before Auth::routes();
Route::get('/employer/login', function () {
  // dd('test');
  return view('auth.employer_auth.login_employeer');
})->name('empLogin');

 Route::post('/employer/empDashboard',[EmployerAuthController::class,'login'])->name('employer.login');//
Snapey's avatar

your middleware is still messed up. You should be checkin authorization only. Not trying to login.

1 like

Please or to participate in this conversation.