Auth :: guard('memberweb') - > user()在重定向后返回null

时间:2018-05-13 19:08:07

标签: laravel-5.5 laravel-middleware

我正在使用laravel-modules软件包“https://nwidart.com/laravel-modules/v3/introduction”,以便更好地构建我的应用程序。

我有一个用Artisan命令创建的用户模型“make :: auth”位于App中,而一个Member模型位于Modules文件夹中。 enter image description here

问题是我登录成员后卫并重定向到/ member / profile Auth :: guard('memberweb') - > user()总是返回null。 但是如果我在MemberLoginController的login方法中dd(Auth :: guard('memberweb') - > user()):

/**
 * Member Login
 *
 * @return void
 */
public function login(Request $request)
{
      $this->validate($request, [
          'email' => 'required|email',
          'password' => 'required',
      ]);

    // Attempt to log the member in
      if (Auth::guard('memberweb')->attempt(['email' => $request->email, 'password' => $request->password], $request->remember)) {
        // if successful, then redirect to their intended location
        dd(Auth::guard('memberweb')->user());
        return redirect()->intended(route('memberProfile'));
      }
      // if unsuccessful, then redirect back to the login with the form data
      return redirect()->back()->withInput($request->only('email', 'remember'))->with('error','your credentials donnot match our records');
}

按预期返回对象。它看起来像被重定向后会员注销。

我的config.auth.guards看起来像这样:

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
    'memberweb' => [
        'driver' => 'session',
        'provider' => 'members',
    ],

    'api' => [
        'driver' => 'token',
        'provider' => 'users',
    ],
    'memberweb-api' => [
        'driver' => 'token',
        'provider' => 'members',
    ],
],

我的Kernel.php:

protected $routeMiddleware = [
    'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
    'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
    'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
    'can' => \Illuminate\Auth\Middleware\Authorize::class,
    'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
    'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
    'memberAuth' => \Modules\Member\Http\Middleware\RedirectIfMemberAuthenticated::class,
    'role' => \App\Http\Middleware\Roles::class,
    'permission' => \App\Http\Middleware\Permissions::class,
];

我做错了什么?

1 个答案:

答案 0 :(得分:0)

问题出在routes.php文件中。您还需要在身份验证之前添加网络防护。

例如,我的route.php看起来像这样:

<?php

Route::group(['middleware' => ['auth','role:developer|admin']], 
function()
{
Route::get('/dashboard/users', 'Modules\User\Http\Controllers\UserController@index')->name('dashboardUsers');
Route::get('/dashboard/user/{id}/edit', 'Modules\User\Http\Controllers\UserController@edit')->name('dashboardEditUserView');
Route::post('/dashboard/user/{id}/edit', 'Modules\User\Http\Controllers\UserController@update')->name('dashboardEditUserView');
});

但应如下所示:

<?php

Route::group(['middleware' => ['web','auth','role:developer|admin']], 
function()
{
Route::get('/dashboard/users', 'Modules\User\Http\Controllers\UserController@index')->name('dashboardUsers');
Route::get('/dashboard/user/{id}/edit', 'Modules\User\Http\Controllers\UserController@edit')->name('dashboardEditUserView');
Route::post('/dashboard/user/{id}/edit', 'Modules\User\Http\Controllers\UserController@update')->name('dashboardEditUserView');
});