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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀