I
I
InsaneUser2016-12-14 21:49:05
Laravel
InsaneUser, 2016-12-14 21:49:05

How to fix Unauthenticated error in Laravel 5.3?

Good day to all!
I ask you not to kick hard on crutches, Laravel began to study quite recently)
Task: to make authentication for several types of users.
What was done:

  1. Created tables for each type of user
  2. Created models for each entity
  3. Created login and registration controllers for each entity
  4. Created master controllers for each entity
  5. Created middleware and registered in Kernel.php
  6. Guards are registered in auth.php
  7. Routes are registered

The code below is presented for one type of user, for the rest everything is identical.
Model:
<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Support\Facades\Validator;

class Patient extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'surname', 'birth_date','email', 'password', 'role'
    ];

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

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected static function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:25|confirmed',
        ]);
    }
}

LoginController:
<?php

namespace App\Http\Controllers\AuthPatient;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Auth;

class LoginController extends Controller
{
    use AuthenticatesUsers;

    protected $redirectTo = '/home';
    protected $redirectAfterLogout = '/home';

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

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

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

RegisterController
<?php

namespace App\Http\Controllers\AuthPatient;

use App\Patient;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
//use \App\Http\Controllers\Auth\RegisterController as MainRegisterController;

class RegisterController extends Controller
{
    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param array
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'name' => 'required|max:255',
            'email' => 'required|email|max:255|unique:users',
            'password' => 'required|min:6',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array
     * @return Patient
     */
    protected function create(array $data)
    {
        return Patient::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);
    }
}

Routes:
Route::get('/patient','[email protected]');
    Route::get('/patient/login',['as' => 'patient.login','uses' => 'AuthPatient\[email protected]']);
    Route::post('/patient/login',['uses' => 'AuthPatient\[email protected]']);
    Route::get('/patient/logout',['as' => 'patient.logout','uses' => 'AuthPatient\[email protected]']);

When trying to register and/or login: a new entry is created in the corresponding table, but login does not occur. An Unauthenticated exception is thrown by the Illuminate\Auth\Middleware\Authenticate.php script. Auth::user() is NULL and the $guards array is empty.
Code throwing this exception:
/**
     * Determine if the user is logged in to any of the given guards.
     *
     * @param  array  $guards
     * @return void
     *
     * @throws \Illuminate\Auth\AuthenticationException
     */
    protected function authenticate(array $guards)
    {
        if (empty($guards)) {
            return $this->auth->authenticate();
        }

        foreach ($guards as $guard) {
            if ($this->auth->guard($guard)->check()) {
                return $this->auth->shouldUse($guard);
            }
        }

        throw new AuthenticationException('Unauthenticated.', $guards);
    }

When connecting the native, in combination with the default, guard 'web' everything works correctly - an entry is created in the users table and login occurs.
Tell me, good people, what and where should I fix?
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vlad, 2016-12-15
@Result007

Hey! Maybe try changing the model to Patient instead of User in config/auth.php?
fb45aec17d4648af9dc098d3009c2f4c.png

I
InsaneUser, 2016-12-15
@InsaneUser

Thanks for the answer! Perhaps this would solve the problem if there was one non-default entity, and I have four of them ...

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

        'admin' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],

        'clinic' => [
            'driver' => 'eloquent',
            'model' => App\Clinic::class,
        ],

        'doc' => [
            'driver' => 'eloquent',
            'model' => App\Doc::class,
        ],

        'patient' => [
            'driver' => 'eloquent',
            'model' => App\Patient::class,
        ],

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question