如何在没有Hesto包的情况下在laravel 5.3中创建多个auth?

时间:2017-01-19 15:14:16

标签: laravel laravel-5.3

在Laravel 5.2中,我用帖子做了多次授权:

can-anyone-explain-laravel-5-2-multi-auth-with-example

但是在Laravel 5.3中是威慑力。

我需要在没有Hesto package的Laravel 5.3中创建多个身份验证。

3 个答案:

答案 0 :(得分:1)

要在laravel 5中使用Multi-auth,您必须在auth配置文件中配置警卫。

例如,如果您想拥有一个users表和一个admins表,则必须更改config文件夹中auth.php文件中的以下行。

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

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

然后

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

此外,您还必须为忘记密码表添加配置:

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
    'admins' => [
        'provider' => 'admins',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

然后在您的auth控制器中,您必须添加以下属性:

protected $guard = 'admins'

在admins auth控制器和

protected $guard = 'users'
用户auth控制器中的

然后它应该工作,从现在开始每次调用auth facade都必须包含guard方法,例如获取你以前用过的用户:

Auth::user();

现在它看起来像:

Auth::guard('users')->user();

管理员也一样

Auth::guard('admins')->user();

答案 1 :(得分:1)

对于multi auth中的laravel5.3系统,您需要手动管理所有内容,例如Guard,每次登录的路由。

只需查看以下链接,它对multi auth有用。 希望它可以帮到你。

  

https://github.com/jayminpanchal/laravel-multiauth-demo

答案 2 :(得分:1)

laravel 5.3中的完整工作示例:

Laravel 5.3使用forntend和backend / admin进行多功能验证简单配置

如果您想拥有用户表和管理员表,则必须更改config文件夹中auth.php文件中的以下行。

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

然后

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

此外,您还必须为忘记密码表添加配置:

'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
        'admin' => [
            'provider' => 'admin',
            'email' => 'admin.auth.emails.password',
            'table' => 'admin_password_resets',
            'expire' => 60,
        ],
    ],

路由/ web.php

//Routes for admin...
Route::group(array('prefix' =>'admin','middleware'=>array('web')), function () {

    Route::get('/admin/login','AdminAuth\AuthController@showLoginForm');
    Route::post('/admin/login','AdminAuth\AuthController@login');
    Route::get('/admin/logout','AdminAuth\AuthController@logout');


    Route::get('admin/register', 'AdminAuth\AuthController@showRegistrationForm');
    Route::post('admin/register', 'AdminAuth\AuthController@register');
    Route::get('/admin', 'AdminController@index');
});  

AdminAuth \ AuthController.php 添加两个方法并指定redirectTo和guard

protected $redirectTo = '/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');
} 

protected function guard()
{
    return Auth::guard('admin');
}

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

现在在kernel.php中注册中间件

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

在AdminController.php中使用此中间件

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

我们可以使用Auth :: user()直接访问经过身份验证的用户,但如果您有两个身份验证表,则必须使用

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

对于经过身份验证的管理员使用

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

在文件App / Http / Controllers / AdminAuth / PasswordController.php中添加两个函数

 //For guard
    protected function guard()
    {
        return Auth::guard('admin');
    }

    //For letting laravel know which config you're going to use for resetting password
    public function broker()
    {
        return Password::broker('admin');
    }

并添加三个公共方法showResetForm,getEmail(),showLinkRequestForm

public function getEmail()
{
    return $this->showLinkRequestForm();
}

public function showLinkRequestForm()
{
    if (property_exists($this, 'linkRequestView')) {
        return view($this->linkRequestView);
    }

    if (view()->exists('admin.auth.passwords.email')) {
        return view('admin.auth.passwords.email');
    }

    return view('admin.auth.password');
}

public function showResetForm(Request $request, $token = null)
{

    if (is_null($token)) {
        return $this->getEmail();
    }
    $email = $request->input('email');

    if (property_exists($this, 'resetView')) {
        return view($this->resetView)->with(compact('token', 'email'));
    }

    if (view()->exists('admin.auth.passwords.reset')) {
        return view('admin.auth.passwords.reset')->with(compact('token', 'email'));
    }

    return view('admin.passwords.auth.reset')->with(compact('token', 'email'));
}

现在更新路线/ web.php中添加三条路线

Route::post('admin/password/email','AdminAuth\PasswordController@sendResetLinkEmail');
Route::post('admin/password/reset','AdminAuth\PasswordController@reset');
Route::get('admin/password/reset/{token?}','AdminAuth\PasswordController@showResetForm');