在laravel中创建自定义身份验证系统

时间:2016-03-30 10:13:58

标签: laravel laravel-5.2

如何为客户构建身份验证系统?

我在我的管理面板中使用了 laravel 内置身份验证系统,其中已经使用了内置用户模型和用户表。

现在我想为我的客户构建另一个身份验证系统,其中将使用客户模型和客户表。

我如何在 laravel 5.2

中执行此操作

1 个答案:

答案 0 :(得分:0)

如何在Larvel 5.2中实现Multi Auth

如上所述。两个表管理员和用户

Laravel 5.2有一个新的工匠命令。

php artisan make:auth

它将为用户表生成基本的登录/注册路由,视图和控制器。

为简单起见,将管理表作为用户表。

Controller For Admin
app/Http/Controllers/AdminAuth/AuthController
app/Http/Controllers/AdminAuth/PasswordController

(注意:我刚从app / Http / Controllers / Auth / AuthController复制了这些文件)

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' => '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

添加两个方法并指定$ 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 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.php

 protected $routeMiddleware = [
    'admin' => \App\Http\Middleware\RedirectIfNotAdmin::class,
];
use this middleware in AdminController e.g.,
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');
    }
}

这一切都需要让它工作,并获得经过身份验证的管理员使用的json

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

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

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

Auth::guard('guard_name')->user()->logout()
for authenticated user json

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