laravel多用户验证

laravel 5.2更新了一个新特性——多用户类型验证,不再依赖第三方,使用起来更简单。请注意下面几个配置文件中的”guard”关键词,自行领悟

代码:

https://coding.net/u/has/p/laravel_multiple_authentication/git

auth.php配置

修改app/config/auth.php:

 'defaults' => [
 'guard' => 'user',
 'passwords' => 'users',
 ],
 'guards' => [
 'admin' => [
 'driver' => 'session',
 'provider' => 'admins',
 ],
 'user' => [
 'driver' => 'session',
 'provider' => 'users',
 ],
 ],
 'providers' => [
 'users' => [
 'driver' => 'eloquent',
 'model' => App\User::class,
 ],
 'admins' => [
 'driver' => 'eloquent',
 'model' => App\Admin::class,
 ]
 ],

Authenticate配置

稍微修改一下默认的app/Http/Middleware/Authenticate.php配置,声明跳转路径:

 public function handle($request, Closure $next, $guard = null)
 {
 $redirect = 'admin' == $guard ? 'admin/public/login' : 'user/public/login';
 if (Auth::guard($guard)->guest()) {
 if ($request->ajax()) {
 return response('Unauthorized.', 401);
 } else {
 return redirect()->guest($redirect);
 }
 }
 return $next($request);
 }

routes.php配置

修改app/Http/routes.php:

/**
 * 当访问admin/...时,如果未验证则跳转至admin/public/login
 */
Route::group(['middleware' => ['web', 'auth:admin'], 'prefix' => 'admin', 'namespace' => 'Admin'], function () {
 // your admin routes
});
/**
 * 当访问user/...时,如果未验证则跳转至user/public/login
 */
Route::group(['middleware' => ['web', 'auth:user'], 'prefix' => 'user', 'namespace' => 'User'], function () {
 // your user routes
});
Route::group(['middleware' => 'web'], function() {
 Route::controllers([
 'admin/public' => 'Admin\PublicController',
 'user/public' => 'User\PublicController',
 ]);
 // your public routes
});

‘auth:admin’、’auth:user’中的’admin’、’user’为中间件参数,表示调用Authenticate时handle的$guard的参数为’admin’、’user’。

登陆控制器配置

以Admin\PublicController为例:

<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class PublicController extends Controller
{
 public function getLogin() {
 return view('admin.public.login');
 }
 public function postLogin(Request $request) {
 $rules = [
 'email' => 'required',
 'password' => 'required'
 ];
 $this->validate($request, $rules);
if (\Auth::guard('admin')->attempt($request->only(['email', 'password']), $request->has('remember'))) {
 return back()->withInput()->withErrors(['password' => ['login successfully']]);
 }
 return back()->withInput()->withErrors(['password' => ['login failed']]);
 }
 public function getLogout() {
 \Auth::guard('admin')->logout();
 return redirect('admin/public/login');
 }
}

登陆视图文件

<!doctype html>
<html lang="zh-cn">
<head>
 <meta charset="UTF-8">
 <title>laravel</title>
</head>
<body>
@if(count($errors) > 0)
 <ul class="alert alert-error">
 @foreach($errors->all() as $error)
 <li>{{$error}}</li>
 @endforeach
 </ul>
@endif
<form method="post">
 <input type="email" name="email" value="{{old('email')}}" placeholder="your email" /><br>
 <input type="password" name="password" placeholder="your password" /><br>
 {{csrf_field()}}
 <button>submit</button>
</form>
</body>
</html>