Answer the question
In order to leave comments, you need to log in
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:
<?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',
]);
}
}
<?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');
}
}
<?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']),
]);
}
}
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]']);
/**
* 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);
}
Answer the question
In order to leave comments, you need to log in
Hey! Maybe try changing the model to Patient instead of User in config/auth.php?
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 questionAsk a Question
731 491 924 answers to any question