Laravel 5.5无法使用身份验证脚手架登录

时间:2018-07-08 05:05:16

标签: php laravel login controller laravel-blade

安装身份验证脚手架并创建用于存储使用用户名和密码之外的凭据的数据库后,我正在我的应用程序上进行注册-登录功能。我已经成功注册了新帐户,已存储到数据库中,并重定向到“管理”页面。但是注销后,我无法使用我创建的以前的帐户登录,并且显示了这样的错误消息。

Error message that prevent login

我该怎么办?

这是我的:

web.php     

Route::get('/', 'FrontController@home')->name('home');

Route::get('/login', 'FrontController@login')->name('login');

Auth::routes();

Route::get('/home', 'AdminController@index')->name('admin');

Route::get('/manual', 'ManualLoginController@manual')->name('manual');

Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout')->name('logout');

LoginController.php

<?php

namespace App\Http\Controllers\Auth;

use Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

class LoginController extends Controller
{

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function logout(Request $request) {
          Auth::logout();
          return redirect('/login');
    }

}

login.blade.php

<div class="container-fluid-full">         
    <div class="row-fluid">
        <div class="login-box">
            <div class="icons">
                <a href="/"><i class="halflings-icon home"></i></a>
                <a href="#"><i class="halflings-icon cog"></i></a>
            </div>
            <h2 style="font-family: 'Raleway'; font-weight: 800">Masuk sebagai administrator</h2>
            @if ($errors->any()) 
            <div class="alert alert-danger">
                <ul>
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
                    @endforeach
                </ul>
            </div>
            @endif
            <form class="form-horizontal" action="{{route('login')}}" method="POST">
                @csrf
                <fieldset>
                    <div class="input-prepend" title="Username">
                        <label for="username">{{ __('Username') }}</label>

                        <span class="add-on"><i class="halflings-icon user"></i></span>

                        <input class="input-large span10 form-control{{ $errors->has('username') ? ' is-invalid' : '' }}" name="username" id="username" type="text" placeholder="username" value="{{ old('username') }}" required autofocus/>

                        @if ($errors->has('username'))
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $errors->first('username') }}</strong>
                            </span>
                        @endif
                    </div>


                    <div class="clearfix"></div>

                    <div class="input-prepend" title="Password">
                        <label for="password">{{ __('Password') }}</label>

                        <span class="add-on"><i class="halflings-icon lock"></i></span>

                        <input class="input-large span10 form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" id="password" type="password" placeholder="Password" required/>

                        @if ($errors->has('password'))
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $errors->first('password') }}</strong>
                            </span>
                        @endif
                    </div>

                    <div class="clearfix"></div>

                    <div class="button-login">  
                        <button type="submit" class="btn btn-primary"> {{ __('Login') }}
                        </button>
                    </div>
                    <div class="clearfix"></div>
                </fieldset>
            </form>
        </div><!--/span-->
    </div><!--/row-->
</div>

RegisterController.php

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{

    use RegistersUsers;

    /**
     * Where to redirect users after registration.
     *
     * @var string
     */
    protected $redirectTo = '/home';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest');
    }

    /**
     * Get a validator for an incoming registration request.
     *
     * @param  array  $data
     * @return \Illuminate\Contracts\Validation\Validator
     */
    protected function validator(array $data)
    {
        return Validator::make($data, [
            'username' => 'required|string|max:255|unique:users',
            'password' => 'required|string|min:4|confirmed',
        ]);
    }

    /**
     * Create a new user instance after a valid registration.
     *
     * @param  array  $data
     * @return \App\User
     */
    protected function create(array $data)
    {
        return User::create([
            'username' => $data['username'],
            'password' => Hash::make($data['password']),
        ]);
    }
}

FronController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class FrontController extends Controller
{
    public function home () {
        return view ('home');
    }

    public function login () {
        return view ('login');
    }

}

AdminController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AdminController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('admin');
    }
}

1 个答案:

答案 0 :(得分:1)

默认情况下,laravel使用email字段进行身份验证,您可以通过在登录控制器中定义以下功能来覆盖它

public function username()
{
    return 'username';
}

有关详细信息,您可以签入Illuminate\Foundation\Auth\AuthenticatesUsers特性