T
T
Terroris3372021-04-07 13:47:41
Laravel
Terroris337, 2021-04-07 13:47:41

Why is authorization/authentication not working in Laravel 7?

Good day, dear experts. Faced an authentication problem.
There are two tables:
users

Schema::create('users', function (Blueprint $table) {
            $table->id();
            $table->string('fname');
            $table->string('lname');
            $table->string('patronymic');
            $table->date('birth_date');
            $table->enum('gender', ['Мужкой', 'Женский']);
            $table->string('email')->unique();
            $table->string('phone')->unique();
            $table->decimal('rate');
            $table->timestamp('email_verified_at')->nullable();
            $table->boolean('mailing')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });


and employees
Schema::create('employees', function (Blueprint $table) {
            $table->id();
            $table->string('fname', 255);
            $table->string('lname', 255);
            $table->string('pname', 255);
            $table->string('avatar', 255)->nullable();
            $table->date('birth_date');
            $table->string('login', 255)->unique();
            $table->string('password', 255);
            $table->integer('percent')->nullable()->unsigned();
            $table->timestamps();
        });

I eat to make authorization for these users.
auth.php
<?php

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

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

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

        'employee' => [
            'driver' => 'session',
            'provider' => 'employees',
        ],
    ],

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

        'employees' => [
            'driver' => 'eloquent',
            'model' => App\Models\Employee::class,
        ],
    ],

    'passwords' => [
        'users' => [
            'provider' => 'users',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],

        'employees' => [
            'provider' => 'employees',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],
];

EmployeeLoginController
<?php

namespace App\Http\Controllers\Auth;

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

class EmployeeLoginController extends Controller
{
    use AuthenticatesUsers;

    protected $redirectTo = RouteServiceProvider::EMPLOYEE_HOME;

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

    public function showLoginForm()
    {
        return view('auth.employeeLogin');
    }

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

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

    public function logout(Request $request)
    {
        $this->guard()->logout();

        $request->session()->invalidate();

        return redirect()->route('auth.employeeLogin');
    }

    public function guard()
    {
        return Auth::guard('employee');
    }
}

Employee Model
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use LaravelAndVueJS\Traits\LaravelPermissionToVueJS;
use Spatie\Permission\Traits\HasRoles;

class Employee extends Authenticatable
{
    use Notifiable;

    protected $guard_name = 'web';
    protected $gaurd = 'employee';
    protected $guarded = [];

    protected $fillable = ['fname', 'lname', 'pname', 'birth_date', 'login', 'password'];

    protected $hidden = ['password', 'remember_token'];

    protected $casts = [
        'email_verified_at' => 'datetime',
        'birth_date' => 'date:d.m.Y',
    ];

    public function phones()
    {
        return $this->morphMany(Phone::class, 'userable');
    }

    public function emails()
    {
        return $this->morphMany(Email::class, 'userable');
    }
}

routes
Route::get('/dashboard/login', [EmployeeLoginController::class, 'showLoginForm'])->name('loginDashboardForm');
Route::post('/dashboardLogin', [EmployeeLoginController::class, 'login'])->name("loginDashboard");


I am using laravel auth package.
The problem is that the EmployeeLoginController does not log in the employee, does not create a session, although the login and password are sent to the required login route.

Tell me what I did wrong or what I did not do?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question