为什么laravel 6 auth使用自定义防护重定向后会返回false? [英] Why laravel 6 auth returns false after redirecting by using custom guard?

查看:127
本文介绍了为什么laravel 6 auth使用自定义防护重定向后会返回false?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to make auth through laravel package using admins table. In the project directory I added admin guard into 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中的登录控制器

Following is my login controller inside 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');
    }

}

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

And in my Admin.php Model following script is there

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';
}

以下是我包裹中的路线

    $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时,我也需要登录页面.

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.

在尝试登录Auth::check()后,刚尝试登录后,它返回true,但是在dashboardController.php构造函数中返回false.以相同的方式Auth::guard('admin')->user()返回用户的信息,而在dashboardController.php上返回null.

Also right after login attempt when I try Auth::check() it's returns true but same thing returning false in dashboardController.php construct function. In the same way Auth::guard('admin')->user() returns user's info while on dashboardController.php it's returns null.

php artisan route:list的奇怪结果

如您在DashboardController.php构造中所见,我添加了$this->middleware('auth:admin');

As you can see in DashboardController.php construct I added $this->middleware('auth:admin');

因此,当我尝试添加dd(Auth::guard('admin')->user())并在终端php artisan route:list中检入时,它返回null,有时返回false,是否知道为什么会发生这种情况?

So when I try to add dd(Auth::guard('admin')->user()) and then check in terminal php artisan route:list it returns null and sometime false, any idea why it is happening?

我不知道我在想什么,什么地方想念什么.

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.

谢谢

推荐答案

问题出在您的路由文件中:

The problem is in your routes file:

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

您正在将默认防护与auth中间件一起使用.使用admin保护程序登录后,默认的web保护程序可能无法登录.这就是为什么它失败并尝试将您重定向到登录页面的原因:

You are using the default guard with auth middleware. After you are logged in with admin guard you may not be logged in by your default web guard. That is why it fails and tries to redirect you to login page:

当我尝试登录时,提交有效的详细信息后,它没有将我重定向到仪表板,没有任何反应.另外,当我尝试强制打开/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.

相反,您应该在组中指定使用admin防护:

Instead, you should specify in your group that you are using the admin guard:

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

但是,您已经在DashboardController中指定使用$this->middleware('auth:admin');,因此无需在路由组中再次指定它.以下内容就足够了,并减少了产生错误的可能性:

However, you already specified in your DashboardController to use $this->middleware('auth:admin');, so there is no need to specifiy it in the route group again. The following is enough and reduces the likelihood to create an error:

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

这篇关于为什么laravel 6 auth使用自定义防护重定向后会返回false?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆