我想在laravel 5.4中为管理页面创建第二个身份验证 .
First of all let me describe my problem: 我有一个可操作的用户登录(默认laravel auth)通过'web' -guard . 现在我想为管理面板创建第二个身份验证 . 我有另一个表存储名称,一个令牌(类似于密码)和权限级别 .
第二个/单独的表是系统为页面开发的依赖关系,因此我无法更改它 .
I have the login page for the administration panel but when i try to authenticate i get redirected back to the login everytime.
我已经用谷歌搜索了整个事情并且遇到了一些很好的例子:
-
https://jamesmcfadden.co.uk/custom-authentication-in-laravel-with-guards-and-user-service-providers
-
其他链接在pastebin上的控制器粘贴中(下面链接)
但我无法弄明白 .
Here's what i did already:
- 在 config/auth.php 中添加了第二个名为'admin'的 guard
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'admin' => [
'driver' => 'session',
'provider' => 'admin',
]
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\User::class,
],
'admin' => [
'driver' => 'eloquent',
'model' => App\Admin::class,
]
],
- 添加了所需的 model
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Admin extends Authenticatable
{
use Notifiable;
protected $fillable = [
'mID',
'mAccount',
'mName',
'mServerIP',
'mAuthority',
'mToken'
];
protected $hidden = [
'mContactIP', 'mToken'
];
protected $table = 'administration';
protected $connection = 'common';
public $timestamps = false;
public function getAuthIdentifierName()
{
return 'mAccount';
}
}
- 在 routes/web.php 中添加了必要的 routes
Route::group(['prefix' => 'admin'], function () {
Route::get('/login','Auth\ElevationController@showLoginForm')->middleware('web');
Route::post('/login','Auth\ElevationController@elevate');
Route::get('/logout','Auth\ElevationController@demote');
Route::get('/', function (){return redirect('admin/dashboard');});
Route::get('/dashboard', 'AdminController@index');
});
- 在 app/Http/Middleware 下添加了一个名为'RedirectIfElevated ' via the command ' php artisan make:middleware'的新 middleware
public function handle($request, Closure $next, $guard = 'admin')
{
if (!Auth::guard($guard)->check())
{
if(!Auth::guard('web')->check())
{
return redirect('/');
}
return redirect('/admin/login');
}
return $next($request);
}
- 和 Kernel.php
protected $routeMiddleware = [
.
.
.
'admin' => \WarShape\Http\Middleware\RedirectIfElevated::class,
];
-
最后我创建了我的 Controller :https://pastebin.com/s6iJgFAB
-
并创建了 view
@extends('layouts.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">Elevation</div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="{{ url('/admin/login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('password') ? ' has-error' : '' }}">
<label for="mToken" class="col-md-4 control-label">Token</label>
<div class="col-md-6">
<input id="mToken" type="password" class="form-control" name="mToken" required>
@if ($errors->has('password'))
<span class="help-block">
<strong>{{ $errors->first('password') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group{{ $errors->has('g-recaptcha-response') ? ' has-error' : '' }}">
<label for="recaptcha" class="col-md-4 control-label">Solve Captcha <br> & Elevate!</label>
<div class="col-md-6">
{!! app('captcha')->display($attributes = [], $lang = app()->getLocale()) !!}
@if ($errors->has('g-recaptcha-response'))
<span class="help-block">
<strong>{{ $errors->first('g-recaptcha-response') }}</strong>
</span>
@endif
</div>
</div>
<input type="hidden" name="mAccount" value="{{ Auth::guard('web')->user()->login }}">
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Elevate
</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
So the question i need an answer to is:
- 我在哪里错过了什么?我在哪里陷入困境?
我希望你能帮助我,谢谢你的帮助!
2 回答
我回答你的问题,但是你不能在你的用户表中添加一个简单的列,比如
is_admin
并且仅授权用户在哪里is_admin = 1
用中间件访问管理面板,而不是两次登录?我用以下自定义登录方法修复了它: