Laravel 5.7-登录不会记录用户

时间:2019-02-17 00:06:26

标签: php laravel laravel-5.7

三天前,我开始学习有关Laravel的知识,但遇到了无法解决的问题。

我注册用户时,该表单有效,并且用户已登录。

注销也可以,但是当我登录已经注册的用户时,Laravel不会登录。相反,它将用户引导回到首页(或我设置Authenticate.php的任何路由)文件在Authenticate.php目录下)。

下面,我在必要的文件上显示我的代码:

登录视图文件

<form method="POST" action="{{ route('login') }}">
@csrf
    <input type="email" placeholder="example@me.com" class="email form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" name="email" value="" required autofocus>
    @if ($errors->has('email'))
        <span class="invalid-feedback" role="alert">
            <strong>{{ $errors->first('email') }}</strong>
        </span>
    @endif
    <input type="password" placeholder="Password" class="pwd form-control{{ $errors->has('password') ? ' is-invalid' : '' }}" name="password" value="" required>
    @if ($errors->has('password'))
        <span class="invalid-feedback" role="alert">
            <strong>{{ $errors->first('password') }}</strong>
        </span>
    @endif
    @if (Route::has('password.request'))
        <a class="btn btn-link" href="{{ route('password.request') }}">
                                        {{ __('Forgot Your Password?') }}</a>
    @endif
    <div class="remember_me_wrapper input">
        <input type="checkbox" id="remember_me" class="checkbox" name="remember_me" value="scales" />
        <label class="remember_me_label" for="remember_me" {{ old('remember') ? 'checked' : '' }}>
            <span></span>Remember Me
        </label>
    </div>
    <input type="submit" name="submit" class="submit" value="Log in">
</form>

登录控制器文件:

<?php

namespace App\Http\Controllers\Auth;

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

class LoginController extends Controller
{
    use AuthenticatesUsers;


    protected $redirectTo = '/';

    public function __construct()
    {
        $this->middleware('guest')->except('logouts');
    }

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

    public function checkLogin(Request $request){
        $this->validate($request, [
            'email'         => 'required|email',
            'password'      => 'required|min:8',
        ]);
    }
}

路由(web.php)

Route::group(['middleware' => ['web']], function (){

    Route::resource('/', "BlogController");
    Route::get('auth/login', 'Auth\LoginController@getLogin');
    Route::post('auth/login', 'Auth\LoginController@login');
    Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout')->name('logout' );

    //register
    Route::get('auth/register', 'Auth\RegisterController@getRegister');
    Route::post('auth/register', 'Auth\RegisterController@postRegister');

    Auth::routes();

    Route::any('{catchall}', function($page) {
        abort(404);
    } )->where('catchall', '(.*)');

});

我们将不胜感激!

1 个答案:

答案 0 :(得分:0)

__construct()方法中,应为logout而不是logouts或在检查用户凭据时是否对密码进行了哈希处理。 希望这会有所帮助:)