[英]How can I use auth for custom guard in my package using laravel 6?
I am trying to make auth through laravel package using admins
table.我正在尝试使用admins
表通过 laravel 包进行身份验证。 In the project directory I added admin
guard into config/auth.php在项目目录中,我将admin
守卫添加到 config/auth.php
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admins' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
],
// 'users' => [
// 'driver' => 'database',
// 'table' => 'users',
// ],
],
And in the guard array并且在守卫阵列中
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
Following is my login controller inside pacakge以下是我在 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!');
}
}
}
and following is my dashboard controller以下是我的仪表板控制器
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');
}
}
And in my Admin.php
Model following script is there在我的Admin.php
模型中,有以下脚本
namespace App;
class Admin extends \ABC\xyz\App\Models\Admin
{
}
Which is extending package model哪个是扩展包模型
namespace ABC\xyz\App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
protected $table = 'admins';
}
And below are the routes from my package以下是我的包裹中的路线
$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');
});
When I try to login, after submitting valid details it does not redirecting me to dashboard, nothing happening.当我尝试登录时,在提交有效详细信息后它不会将我重定向到仪表板,什么也没有发生。 Also when I try for open forcefully /dashboard it take me to login page.此外,当我尝试强制打开 /dashboard 时,它会带我进入登录页面。
Also right after login attempt when I try Auth::check()
it's returns true
but same thing returning false
in dashboardController.php
construct function.同样在我尝试登录尝试Auth::check()
之后,它返回true
,但在dashboardController.php
构造函数中返回false
也是一样的。 In the same way Auth::guard('admin')->user()
returns user's info while on dashboardController.php
it's returns null
.以同样的方式Auth::guard('admin')->user()
返回用户的信息,而在dashboardController.php
它返回null
。 I don't know what and where I am missing something.我不知道我在哪里遗漏了什么。
I would like to request you kindly guide me about it.我想请您指导我。 I would appreciate.我将不胜感激。
Thank you谢谢
When you define a route with a Prefix, the route name will be like prefix.name
and the url will be like prefix/url
.当您使用前缀定义路由时,路由名称将类似于prefix.name
并且 url 将类似于prefix/url
。 So here you can try this所以在这里你可以试试这个
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');
});
All group was prefixed by "admin" , but there are some route or page that can be visited when its logged in.所有组都以“admin”为前缀,但是登录时可以访问一些路由或页面。
The prefix
are an option for defining a prefix the routes endpoint. prefix
是定义路由端点前缀的选项。 You are looking for the as
option.您正在寻找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');
});
Run php artisan optimize:clear
after code changes.在代码更改后运行php artisan optimize:clear
。
The guest
middleware ( \App\Http\Middleware\RedirectIfAuthenticated
) is responsible for redirecting authenticated users. guest
中间件 ( \App\Http\Middleware\RedirectIfAuthenticated
) 负责重定向经过身份验证的用户。
When using the middleware, you have to pass it the authentication guard to be used like this:使用中间件时,您必须将身份验证保护传递给它才能使用,如下所示:
guest:guard
Example: in your LoginController constructor, use示例:在您的 LoginController 构造函数中,使用
$this->middleware('guest:admin')->except('logout');
instead of $this->middleware('guest')->except('logout');
而不是$this->middleware('guest')->except('logout');
use auth('admin')->user()
in your dashboard controller.在仪表板控制器中使用auth('admin')->user()
。 (returns authenticated admin) (返回经过身份验证的管理员)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.