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 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题
  • ¥15 求C# .net4.8小报表工具
  • ¥15 安装虚拟机时出现问题
  • ¥15 Selenium+docker Chrome不能运行
  • ¥15 mac电脑,安装charles后无法正常抓包
  • ¥18 visio打开文件一直显示文件未找到