内容纲要

通过我们需要前端和后台界面分开认证

修改 config/auth.php

[code language=”php”]
// Authenticating guards

‘guards’ => [
‘web’ => [
‘driver’ => ‘session’,
‘provider’ => ‘users’,
],
‘api’ => [
‘driver’ => ‘token’,
‘provider’ => ‘users’,
],
‘admin’ => [
‘driver’ => ‘session’,
‘provider’ => ‘admins’,
],
],

// Providers

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

[/code]

简单的认证方式为

[code language=”php”]
$user = Auth::user();
dd($user);
[/code]

多种方式的认证

[code language=”php”]
$user = Auth::guard(‘admin’)->user();
// Or…
$user = auth()->guard(‘admin’)->user();
dd($user);
[/code]

建立用户模型

app/User.php

[code language=”php”]
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
‘name’, ’email’, ‘password’,
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
‘password’, ‘remember_token’,
];
}
[/code]

app/Admin.php

[code language=”php”]
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
‘name’, ’email’, ‘password’,
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
‘password’, ‘remember_token’,
];
}
[/code]

修改路由

[code language=”php”]
Route::group([‘middleware’ => [‘web’]], function () {
Route::get(‘login’, ‘UserLoginController@getUserLogin’);
Route::post(‘login’, [‘as’=>’user.auth’,’uses’=>’UserLoginController@userAuth’]);
Route::get(‘admin/login’, ‘AdminLoginController@getAdminLogin’);
Route::post(‘admin/login’, [‘as’=>’admin.auth’,’uses’=>’AdminLoginController@adminAuth’]);
Route::group([‘middleware’ => [‘admin’]], function () {
Route::get(‘admin/dashboard’, [‘as’=>’admin.dashboard’,’uses’=>’AdminController@dashboard’]);
});
});
[/code]

创建一个管理员认证中间件 RedirectIfNotAdmin

[code language=”php”]
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
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(‘admin/login’);
}
return $next($request);
}
}
[/code]

在 kernal.php 中注册中间件

[code language=”php”]
protected $routeMiddleware = [
‘admin’ => \App\Http\Middleware\RedirectIfNotAdmin::class,
];
[/code]

app/Http/Controllers/UserLoginController.php

[code language=”php”]
<?php
namespace App\Http\Controllers;
use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class UserLoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = ‘/’;
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware(‘guest’, [‘except’ => ‘logout’]);
}
public function getUserLogin()
{
return view(‘userLogin’);
}
public function userAuth(Request $request)
{
$this->validate($request, [
’email’ => ‘required|email’,
‘password’ => ‘required’,
]);
if (auth()->attempt([’email’ => $request->input(’email’), ‘password’ => $request->input(‘password’)]))
{
$user = auth()->user();
dd($user);
}else{
dd(‘your username and password are wrong.’);
}
}
}
[/code]

app/Http/Controllers/AdminLoginController.php

[code language=”php”]
<?php
namespace App\Http\Controllers;
use App\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class AdminLoginController extends Controller
{
use AuthenticatesUsers;
protected $redirectTo = ‘/’;
/**
* Create a new authentication controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware(‘guest’, [‘except’ => ‘logout’]);
}
public function getAdminLogin()
{

if (auth()->guard(‘admin’)->user()) return redirect()->route(‘admin.dashboard’);
return view(‘adminLogin’);
}
public function adminAuth(Request $request)
{
$this->validate($request, [
’email’ => ‘required|email’,
‘password’ => ‘required’,
]);
if (auth()->guard(‘admin’)->attempt([’email’ => $request->input(’email’), ‘password’ => $request->input(‘password’)]))
{
return redirect()->route(‘admin.dashboard’);
}else{
dd(‘your username and password are wrong.’);
}
}
}
[/code]

app/Http/Controllers/AdminController.php

[code language=”php”]
<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class AdminController extends Controller
{

public function dashboard(){
$user = auth()->guard(‘admin’)->user();
dd($user);
}
}
[/code]

创建视图

resources/views/app.blade.php

[code language=”html”]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Laravel 5.4 – Multi Auth </title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
@yield(‘content’)
</body>
</html>
[/code]

resources/views/userLogin.blade.php

[code language=”html”]
@extends(‘app’)
@section(‘content’)
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">User Login</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route(‘user.auth’) }}">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has(’email’) ? ‘ has-error’ : ” }}">
<label class="col-md-3 control-label">E-Mail</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old(’email’) }}">
@if ($errors->has(’email’))
<span class="help-block">
<strong>{{ $errors->first(’email’) }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has(‘password’) ? ‘ has-error’ : ” }}">
<label class="col-md-3 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
@if ($errors->has(‘password’))
<span class="help-block">
<strong>{{ $errors->first(‘password’) }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<button type="submit" class="btn btn-primary">Login</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
[/code]

resources/views/adminLogin.blade.php

[code language=”html”]
@extends(‘app’)
@section(‘content’)
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Admin Login</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ route(‘admin.auth’) }}">
{!! csrf_field() !!}
<div class="form-group{{ $errors->has(’email’) ? ‘ has-error’ : ” }}">
<label class="col-md-3 control-label">E-Mail</label>
<div class="col-md-6">
<input type="email" class="form-control" name="email" value="{{ old(’email’) }}">
@if ($errors->has(’email’))
<span class="help-block">
<strong>{{ $errors->first(’email’) }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has(‘password’) ? ‘ has-error’ : ” }}">
<label class="col-md-3 control-label">Password</label>
<div class="col-md-6">
<input type="password" class="form-control" name="password">
@if ($errors->has(‘password’))
<span class="help-block">
<strong>{{ $errors->first(‘password’) }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-3">
<button type="submit" class="btn btn-primary">Login</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
[/code]

发表评论

电子邮件地址不会被公开。 必填项已用*标注