如何使用laravel 6在包中使用auth进行自定义防护?

时间:2020-02-07 07:46:59

标签: php laravel laravel-6

我正在尝试使用admins表通过laravel软件包进行身份验证。在项目目录中,我将admin保护加入了config / auth.php

'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],

        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],

        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],

在保护数组中

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

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
            'hash' => false,
        ],

        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
    ],

以下是我在pacakge中的登录控制器

class LoginController extends Controller
{

   use AuthenticatesUsers;
   protected $redirectTo = '/admin/dashboard';
   protected function redirectTo()
   {
         return '/admin/dashboard';
   }

   public function __construct()
   {
       $this->middleware('guest')->except('logout');
   }
   public function login(Request $request)
   {   
       if(Auth::guard('admin')->attempt($request->only('email','password'), true)){
           return redirect()
               ->intended(route('dashboard'))
               ->with('status','You are Logged in as Admin!');
       }
   }

}

以下是我的仪表板控制器

class DashboardController extends Controller
{
    public function __construct()
    {
        /* dd(Auth::check()); */ //return false : just want to show you

          $this->middleware('auth:admin');
    }

    public function index()
    {
        return view('xyz::dashboard');
    }

}

在我的Admin.php模型中,存在以下脚本

namespace App;

class Admin extends \ABC\xyz\App\Models\Admin
{

}

哪个扩展了包装模型

namespace ABC\xyz\App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{

    protected $table = 'admins';
}

以下是我包裹中的路线

    $namespace = 'ABC\Xyz\App\Http\Controllers';
    Route::group([    
    'namespace' => $namespace,
    'middleware' => ['web'], 
    'prefix' => 'admin'
], function () {
    Route::get('login', function(){
        return view('xyz::auth.login');
    })->name('login');

    Route::post('/login', 'Auth\LoginController@login')->name('customLogin');
});

Route::group(['namespace' => $namespace,'prefix' => 'admin',  'middleware' => ['auth']  ], function () {
    Route::get('dashboard', 'DashboardController@index')->name('dashboard');
});

当我尝试登录时,提交有效的详细信息后,它没有将我重定向到仪表板,没有任何反应。另外,当我尝试强制打开/ dashboard时,我也需要登录页面。

登录尝试后,当我尝试Auth::check()时,它返回true,但在false构造函数中返回dashboardController.php也是一样。以同样的方式Auth::guard('admin')->user()返回用户的信息,而在dashboardController.php上返回用户的信息null。我不知道我在想什么,在哪里想念什么。

我想请您指导我。我会感激的。

谢谢

3 个答案:

答案 0 :(得分:0)

使用前缀定义路由时,路由名称将类似于prefix.name,而网址将类似于prefix/url。所以您可以在这里尝试

Route::group([    
        'namespace' => $namespace,
        'middleware' => ['web'], 
        'prefix' => 'admin'
    ], function () {
        Route::get('login', function(){
            return view('xyz::auth.login');
        })->name('login');

        Route::post('login', 'Auth\LoginController@login')->name('tryForLogin');
});

Route::group(['namespace' => $namespace,'prefix' => 'admin','middleware' => 'auth'], function () {
    Route::get('dashboard', function(){
        return view('xyz::dashboard');
    })->name('dashboard');
});

所有组均以“ admin”作为前缀,但登录后可以访问某些路由或页面。

答案 1 :(得分:0)

prefix是用于定义路由端点的前缀的选项。您正在寻找as选项。

// gives you routes:
// GET /admin/login named "admin.login" with middleware "web"
// POST /admin/login named "admin.tryForLogin" with middleware "web"
Route::group([    
        'namespace' => $namespace,
        'middleware' => ['web'], 
        'prefix' => 'admin',
        'as' => 'admin.'
    ], function () {
        Route::get('login', function() {
            return view('xyz::auth.login');
        })
        ->name('login');

        Route::post('login', 'Auth\LoginController@login')->name('tryForLogin');
    }
);

// gives you routes:
// GET /admin/dashboard named "admin.dashboard" with middleware "auth"
Route::middleware(['auth'])->group(function () {
    Route::get('/admin/dashboard', function(){
        return view('xyz::dashboard');
    })
    ->name('admin.dashboard');
});

代码更改后运行php artisan optimize:clear

答案 2 :(得分:0)

guest中间件(\App\Http\Middleware\RedirectIfAuthenticated)负责重定向经过身份验证的用户。

使用中间件时,必须向其传递身份验证保护,以使其使用:

guest:guard

示例:在LoginController构造函数中,使用

$this->middleware('guest:admin')->except('logout');

代替$this->middleware('guest')->except('logout');