Laravel 5.6 - 用户未通过身份验证

时间:2018-06-06 07:45:35

标签: php laravel eloquent laravel-5.6

我已经找了好几天,但是在使用auth中间件时我的用户从未经过身份验证。

我们不使用标准的laravel方式,而是使用标准的auth中间件来阻止对路由组的访问。 (在本例中为仪表板)

数据是正确的,因为它不会给出任何错误消息,但用户本身永远不会得到身份验证。自上周三以来我一直坚持下去,我希望你们中的任何人都能解决这个问题。

实际上没有任何特定的代码,但我的(尝试)登录用户总是被定向到未经身份验证的方法。 (这导致我的另一条路线,索引)

用于登录的重要数据库列称为“用户名”和“密码”(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中间件保护的仪表板路由

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,
    ],

1 个答案:

答案 0 :(得分:0)

Laravel模型默认为id作为Models中的列名。如果您使用与id不同的任何内容,则应使用表的主键名称设置protected属性$primaryKey。例如:

protected $primaryKey = 'Leerling_ID';
相关问题