Как создать пользовательскую аутентификацию laravel 5.5?

Я создал собственный контроллер аутентификации в Laravel 5.5 с действием «сохранить» внутри него, затем я аутентифицировал его, используя метод $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>

person Fred    schedule 21.11.2017    source источник
comment
Не могли бы вы включить свой шаблон auth.index?   -  person Jeffrey    schedule 21.11.2017
comment
Окей, сделано. Извините за задержку с ответом.   -  person Fred    schedule 23.11.2017


Ответы (1)


Для метода попытки требуется параметр $request as, иначе вам потребуется включить маркер запоминания в $data. Я думаю, у охранника отсутствует жетон запоминания.

person synthor    schedule 27.03.2018