简体   繁体   English

使用 Laravel 5.2 中的单用户表进行多重身份验证

[英]Multi Auth with single user table in laravel 5.2

I want to Multi Auth with single table in laravel 5.2.我想在 laravel 5.2 中使用single表进行多重身份验证。

I tried this way.我试过这种方式。 But frontend login working.但前端登录工作。

Laravel 5.2 has a new artisan command. Laravel 5.2 有一个新的artisan命令。

php artisan make:auth

it will generate basic login/register route , view and controller for user table.它将为user表生成基本的登录/注册routeviewcontroller

Make a admin table as users table for simplicity.为简单起见,将admin表作为users表。

Controller For Admin管理员控制器
app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController
(note: I just copied these files from app/Http/Controllers/Auth/AuthController here) (注意:我只是从app/Http/Controllers/Auth/AuthController这里复制了这些文件)

config/auth.php

//Authenticating guards

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

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

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

route.php

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

});  

AdminAuth/AuthController.php

Add two methods and specify $redirectTo and $guard添加两个方法并指定$redirectTo$guard

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

it will help you to open another login form for admin它将帮助您为管理员打开另一个登录表单

creating a middleware for adminadmin创建一个中间件

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.phpkernel.php注册中间件

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

use this middleware in AdminController eg,AdminController使用这个中间件,例如,

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

In your AdminAuth/AuthController.php .在您的AdminAuth/AuthController.php

   public function guard()
    {
        return auth()->guard('admin');
    }

and instead of give table name inside providers put protected $table = 'users' in your Admin model.而不是在提供者中提供表名,而是将protected $table = 'users'放在您的 Admin 模型中。 You can see this for further more details: https://scotch.io/@sukelali/how-to-create-multi-table-authentication-in-laravel您可以查看更多详细信息: https : //scotch.io/@sukelali/how-to-create-multi-table-authentication-in-laravel

You don't need to use a guard here, Instead;你不需要在这里使用警卫,而是; you can use middleware here.你可以在这里使用中间件。 Add a user type column here in user table and then create middleware for that.在用户表中添加一个用户类型列,然后为此创建中间件。

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

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