dqoeghe9452 2018-06-06 07:45
浏览 99
已采纳

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

I have looked for days, but my user never gets authenticated when using the auth middleware.

We don't use the standard laravel way, but using the standard auth middleware to block access to a route group. (in this example the dashboards)

The data is correct as it doesnt give any error message, but the user itself never gets authenticated. I've been stuck at it since wednesday and I hope any of you can finally fix this problem.

There isn't really any specific code, but my (try to) login user always get directed to the unauthenticated method. (which leads back to another route of mine, index)

Important database columns for login are called 'username' and 'password' (the standard name laravel uses)

So, please, if any of you can help, I'd be hella grateful!

Inside LeerlingController (where login function is)

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');
}

Inside Leerling Model (the part to make it authenticatable)

use Illuminate\Auth\Authenticatable;

class Leerling extends Model implements \Illuminate\Contracts\Auth\Authenticatable {
use Authenticatable;

Method that keeps happening while logging in a valid user as no fail message comes up Which is located in App\Exceptions\Handler.php

protected function unauthenticated($request, AuthenticationException $exception)
{
    return $request->expectsJson()
        ? response()->json(['message' => $exception->getMessage()], 401)
        : redirect()->route('index');
}

The dashboard routes protected by auth middleware

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条回答 默认 最新

  • dongyi4420 2018-06-06 08:59
    关注

    Laravel models default to id as a column name in Models. If you use anything different that id, you should set a protected property $primaryKey with the name of your table's primary key. Eg:

    protected $primaryKey = 'Leerling_ID';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 matlab解优化问题代码
  • ¥15 写论文,需要数据支撑
  • ¥15 identifier of an instance of 类 was altered from xx to xx错误
  • ¥100 反编译微信小游戏求指导
  • ¥15 docker模式webrtc-streamer 无法播放公网rtsp
  • ¥15 学不会递归,理解不了汉诺塔参数变化
  • ¥15 基于图神经网络的COVID-19药物筛选研究
  • ¥30 软件自定义无线电该怎样使用
  • ¥15 R语言mediation包做中介分析,直接效应和间接效应都很小,为什么?
  • ¥15 Jenkins+k8s部署slave节点offline