【问题标题】:How to create a laravel 5.5 custom auth?如何创建 laravel 5.5 自定义身份验证?
【发布时间】:2017-11-21 12:56:34
【问题描述】:

我在 Laravel 5.5 中创建了一个自定义身份验证控制器,其中包含“存储”操作,然后我使用返回 true$auth->attempt() 方法对其进行了身份验证。到目前为止一切顺利,当我尝试对面板路由使用“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>

【问题讨论】:

  • 你能把你的auth.index模板包括进来吗?
  • 好的,完成。很抱歉延迟回复。

标签: laravel laravel-5.5 custom-authentication


【解决方案1】:

尝试方法需要 $request 作为参数,否则您需要在 $data 中包含记忆标记。我想,守卫缺少记忆令牌。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-05
    • 2019-07-01
    • 1970-01-01
    • 2015-02-23
    • 2016-07-18
    • 2015-11-21
    • 1970-01-01
    相关资源
    最近更新 更多