Laravel 5.6 - 用户未通过身份验证
Laravel 5.6 - User not getting authenticated
我找了好几天,但我的用户在使用 auth 中间件时从未通过身份验证。
我们不使用标准的laravel方式,而是使用标准的auth中间件来阻止对路由组的访问。 (在这个例子中是仪表板)
数据是正确的,因为它没有给出任何错误消息,但用户本身从未通过身份验证。我从星期三开始就一直在坚持,我希望你们中的任何一个都能最终解决这个问题。
确实没有任何特定代码,但我的(尝试)登录用户总是被定向到未经身份验证的方法。 (这又回到了我的另一条路线,索引)
用于登录的重要数据库列称为 'username' 和 'password'(标准名称 laravel 使用)
所以,如果有人能提供帮助,我将不胜感激!
LeerlingController内部(登录功能所在)
public function postLogin(Request $request)
{
$this->validate($request, [
'username' => 'required',
'password' => 'required'
]);
if (!Auth::attempt(['username' => $request->username, 'password' => $request->password])){
return redirect()->back()->with(['fail' => 'We konden je niet inloggen als '.$request->username]);
}
return redirect()->route('leerling.dashboard');
}
Leerling 模型内部(使其可验证的部分)
use Illuminate\Auth\Authenticatable;
class Leerling extends Model implements \Illuminate\Contracts\Auth\Authenticatable {
use Authenticatable;
在登录有效用户时不断出现的方法,因为没有出现失败消息
位于 App\Exceptions\Handler.php
protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->route('index');
}
受auth中间件保护的dashboard路由
Route::group(['middleware' => 'auth'], function () {
Route::get('/leerling/dashboard', [
'uses' => 'LeerlingController@getDashboard',
'as' => 'leerling.dashboard'
]);
// Don't mind this one, it's for the admin dashboard
Route::get('/admin/dashboard', [
'uses' => 'AdminController@getDashboard',
'as' => 'admin.dashboard'
]);
});
Auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Leerling::class,
],
Laravel 模型默认为 id
作为 Models
中的列名称。如果您使用与 id
不同的任何内容,您应该使用 table 的主键名称设置 protected
属性 $primaryKey
。例如:
protected $primaryKey = 'Leerling_ID';
我找了好几天,但我的用户在使用 auth 中间件时从未通过身份验证。
我们不使用标准的laravel方式,而是使用标准的auth中间件来阻止对路由组的访问。 (在这个例子中是仪表板)
数据是正确的,因为它没有给出任何错误消息,但用户本身从未通过身份验证。我从星期三开始就一直在坚持,我希望你们中的任何一个都能最终解决这个问题。
确实没有任何特定代码,但我的(尝试)登录用户总是被定向到未经身份验证的方法。 (这又回到了我的另一条路线,索引)
用于登录的重要数据库列称为 'username' 和 'password'(标准名称 laravel 使用)
所以,如果有人能提供帮助,我将不胜感激!
LeerlingController内部(登录功能所在)
public function postLogin(Request $request)
{
$this->validate($request, [
'username' => 'required',
'password' => 'required'
]);
if (!Auth::attempt(['username' => $request->username, 'password' => $request->password])){
return redirect()->back()->with(['fail' => 'We konden je niet inloggen als '.$request->username]);
}
return redirect()->route('leerling.dashboard');
}
Leerling 模型内部(使其可验证的部分)
use Illuminate\Auth\Authenticatable;
class Leerling extends Model implements \Illuminate\Contracts\Auth\Authenticatable {
use Authenticatable;
在登录有效用户时不断出现的方法,因为没有出现失败消息 位于 App\Exceptions\Handler.php
protected function unauthenticated($request, AuthenticationException $exception)
{
return $request->expectsJson()
? response()->json(['message' => $exception->getMessage()], 401)
: redirect()->route('index');
}
受auth中间件保护的dashboard路由
Route::group(['middleware' => 'auth'], function () {
Route::get('/leerling/dashboard', [
'uses' => 'LeerlingController@getDashboard',
'as' => 'leerling.dashboard'
]);
// Don't mind this one, it's for the admin dashboard
Route::get('/admin/dashboard', [
'uses' => 'AdminController@getDashboard',
'as' => 'admin.dashboard'
]);
});
Auth.php
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Leerling::class,
],
Laravel 模型默认为 id
作为 Models
中的列名称。如果您使用与 id
不同的任何内容,您应该使用 table 的主键名称设置 protected
属性 $primaryKey
。例如:
protected $primaryKey = 'Leerling_ID';