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?
Apr 19, 2024
13
Level 1
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
});
Please or to participate in this conversation.