简体   繁体   English

如何在 Laravel 中为不同的控制器使用不同的身份验证

[英]How to use different Auth for different controller in Laravel

I am creating a project in laravel.我正在 Laravel 中创建一个项目。 My problem is, Since this is a shopping cart I am using different tables for customer and admins.我的问题是,由于这是一个购物车,我为客户和管理员使用不同的表。 So if request is admin then i want to authenticate from admin table and if it is from store i want to use customer table for authentication.因此,如果请求是管理员,那么我想从管理员表进行身份验证,如果它来自商店,我想使用客户表进行身份验证。 Is is it possible to set auth table for controllers or is it possible to use create multiple authenticator other than the default?是否可以为控制器设置身份验证表,或者是否可以使用默认值以外的创建多个身份验证器?

Multi Auth is a common problem that one can face in Laravel so yes it possible to create it.多重身份验证是一个在 Laravel 中可能面临的常见问题,所以是的,可以创建它。

You can write your own code for this or use some package for this specific functionality.您可以为此编写自己的代码或使用某些包来实现此特定功能。 They are available on github easily.它们在 github 上很容易找到。 Example link .示例链接

There is a very good tutorial for this here which I will use for explanation.有一个很好的教程,这在这里,我将使用的说明。

You will need to create two tables, customers and admin.您将需要创建两个表,customers 和 admin。 The default user table can be used for customers (or other way too).默认用户表可用于客户(或其他方式)。 The make:auth command will create all the routes, controllers and views for the users table auth. make:auth 命令将为用户表 auth 创建所有路由、控制器和视图。

For admin auth, first create an admin table.对于管理员身份验证,首先创建一个管理员表。 Next controllers下一个控制器

app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController

Edit config/auth.php file and do same for admin as given for user, using admin model when required instead of user.编辑 config/auth.php 文件,并对用户执行相同的管理操作,在需要时使用管理模型而不是用户。

//Authenticating guards
'guards' => [
    'user' =>[
        'driver' => 'session',
        'provider' => 'user',
    ],
    'admin' => [
        'driver' => 'session',
        'provider' => 'admin',
    ],
],  

//User Providers
'providers' => [
    'user' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],
    'admin' => [
        'driver' => 'eloquent',
        'model' => App\Admin::class,
    ]
],  

//Resetting Password  
'passwords' => [
    'clients' => [
        'provider' => 'client',
        'email' => 'auth.emails.password',
        'table' => 'password_resets',
        'expire' => 60,
    ],
    'admins' => [
        'provider' => 'admin',
        'email' => 'admin.auth.emails.password',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

Edit route file编辑路由文件

Route::group(['middleware' => ['web']], function () {
    //Login Routes...
    Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
    Route::post('/admin/login','AdminAuth\AuthController@login');
    Route::get('/admin/logout','AdminAuth\AuthController@logout');

    // Registration Routes...
    Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
    Route::post('admin/register', 'AdminAuth\AuthController@register');

    Route::get('/admin', 'AdminController@index');

});

Edit AdminAuth\\AuthController.php file and add functions编辑 AdminAuth\\AuthController.php 文件并添加功能

protected $redirectTo = '/admin';
protected $guard = 'admin';
public function showLoginForm()
{
    if (view()->exists('auth.authenticate')) {
        return view('auth.authenticate');
    }

    return view('admin.auth.login');
}
public function showRegistrationForm()
{
    return view('admin.auth.register');
}

Create middleware for admin为管理员创建中间件

class RedirectIfNotAdmin
{
/**
 * Handle an incoming request.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Closure  $next
 * @param  string|null  $guard
 * @return mixed
 */
public function handle($request, Closure $next, $guard = 'admin')
{
    if (!Auth::guard($guard)->check()) {
        return redirect('/');
    }

    return $next($request);
    }
}

Register middleware in kernel在内核中注册中间件

 protected $routeMiddleware = [
    'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
]; 

Use this middleware in admin controller在管理控制器中使用这个中间件

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class AdminController extends Controller
{
    public function __construct(){
        $this->middleware('admin');
   }
public function index(){
        return view('admin.dashboard');
    }
} 

Now you can use it like现在你可以像这样使用它

Auth::guard('admin')->user()

but not directly like但不直接喜欢

Auth::user()

because we have two auths因为我们有两个身份验证

You Should use ENTRUST (Laravel 5 Package)你应该使用ENTRUST(Laravel 5 包)

But before you need to organize your database structure.但在你需要组织你的数据库结构之前。 For all type of user use your users table, have a separate customer table with foreign key user_id.对于所有类型的用户,使用您的用户表,有一个单独的带有外键 user_id 的客户表。 Assign roles to users , When a user logged in check its role and redirect to their dashboard as per assigned role.为用户分配角色,当用户登录时检查其角色并根据分配的角色重定向到他们的仪表板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM