Как создать кастомную аутентификацию laravel 5.5?
Я создал собственный контроллер Auth в Laravel 5.5 с действием "store" внутри него, а затем аутентифицировал его, используя $auth->attempt()
метод, который возвращает true
, Пока все хорошо, проблема начинается, когда я пытаюсь использовать промежуточное программное обеспечение "auth" для маршрутов моей панели, промежуточное программное обеспечение аутентификации всегда перенаправляет на действие входа в систему.
Маршруты:
Route::group(['middleware' => ['auth', 'web']], function () {
Route::get('/painel/', ['as' => 'painel.dashboard', 'uses' => 'DashboardController@index']);
});
Route::get('/login', ['middleware' => 'web', 'as' => 'login', 'uses' => 'AuthController@index']);
Route::group(['middleware' => ['web']], function () {
Route::get('/painel/auth', ['as' => 'painel.auth.index', 'uses' => 'AuthController@index']);
Route::post('/painel/auth/store', ['as' => 'painel.auth.store', 'uses' => 'AuthController@store']);
});
контроллер:
namespace App\Applications\Painel\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Auth\AuthManager as Auth;
class AuthController extends BaseController
{
/**
* @var Guard
*/
private $auth;
public function __construct(Guard $auth)
{
$this->auth = $auth;
}
public function index()
{
return view('painel::auth.index');
}
public function store(Request $request)
{
$data = $request->only(['email', 'password']);
// This condition always return true, but after Laravel return to action index...
if ($this->auth->attempt($data)) {
return redirect()->route('painel.dashboard');
}
return redirect()->back();
}
}
auth.index:
<!doctype html>
<html lang="pt_BR">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Admin</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
</head>
<body>
<div class="row">
<div class="col-sm-12 col-md-12">
<h2 class="text-center">Admin</h2>
<hr>
</div>
</div>
<div class="row">
<div class="col-sm-4 col-md-2 col-md-offset-5 col-sm-offset-4">
<form action="{{ route('painel.auth.store') }}" method="post" id="login-form">
{!! csrf_field() !!}
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" name="email" id="email" placeholder="Email">
</div>
<div class="form-group">
<label for="password">Senha</label>
<input type="password" class="form-control" name="password" id="password" placeholder="Senha">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary btn-lg btn-block" name="submit" id="submit">Entrar</button>
</div>
</form>
</div>
</div>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</body>
</html>
1 ответ
Для метода попытки требуется $request в качестве параметра, иначе вам нужно будет включить токен запоминания в $data. Думаю, охраннику не хватает жетона запоминания.