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';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿