首页 > 解决方案 > Laravel 身份验证 - 即使凭据正确且与数据库完全相同,用户也无法登录

问题描述

我正在创建一个新项目,在创建系统身份验证所需的所有模块后,我无法登录并转到正确的页面,即使登录凭据正确且与数据库。这是代码。请帮助我确切地知道我哪里出错了,提前谢谢你。

控制器

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Validator;
use Auth;

class AuthController extends Controller
{
    function index() {
        return view('auth/login');
    }

    function checklogin(Request $request) {
        $this->validate($request, [
            'username'=>'required|email',
            'password'=>'required|min:6'
        ]);

        $user_data = array(
            'username'=>$request->get('username'),
            'password'=>$request->get('password')
        );

        if(Auth::attempt($user_data)) {
            return redirect('personnel');
        }else {
            return back()->with('error', 'Wrong Login Details');
        }
    }

    function successlogin() {
        return view('personnel');
    }

    function logout() {
        Auth::logout();
        return redirect('auth');
    }
}

登录.blade.php

@extends('layouts.general-master')

@section('title', 'Login')

@section('content')
<div class="header align-left general-header">{{ __('Login') }}</div>
<div class="body">
    @if(isset(Auth::user()->username))
        <script>window.location = "/personnel";</script>
    @endif

    @if($message = Session::get('error'))
        <div class="alert alert-danger alert-block">
            <button type="button" class="close" data-dismiss="alert">x</button>
            <strong>{{ $message }}</strong>
        </div>
    @endif

    @if(count($errors) > 0)
        <div class="alert alert-danger">
            <ul>
                @foreach($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    <form method="POST" action="{{ url('/auth/checklogin') }}">
        @csrf

        <div class="form-group row">
            <label for="username" class="col-md-4 col-form-label text-md-right">{{ __('Username') }}</label>

            <div class="col-md-6">
                <input id="username" type="email" class="form-control{{ $errors->has('username') ? ' is-invalid' : '' }} general-input" name="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>

        <div class="form-group row">
            <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>

            <div class="col-md-6">
                <input id="password" type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }} general-input" name="password" required>

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

        <div class="form-group row">
            <div class="col-md-6 col-md-push-2">
                <div class="form-check">
                    &emsp;&emsp;
                    <input class="form-check-input" type="checkbox" name="remember" id="remember" {{ old('remember') ? 'checked' : '' }}>
                    <label class="form-check-label" for="remember">
                        {{ __('Remember Me') }}
                    </label>
                </div>
            </div>
        </div>

        <div class="form-group row mb-0">
            <div class="col-md-8 col-md-push-2">
                &emsp;
                <button type="submit" class="btn btn-primary">
                    {{ __('Login') }}
                </button>

                @if (Route::has('password.request'))
                    <a class="btn btn-primary col-white" href="{{ route('password.request') }}">
                        {{ __('Forgot Your Password?') }}
                    </a>
                @endif
            </div>
        </div>
    </form>
</div>
@endsection

person.blade.php -- 这是登录后的正确页面

@extends('layouts.master')

@section('title', 'Personnel Management')

@section('content')
    @if(isset(Auth::user()->username))
        <section class="content">
            <div class="container-fluid">
                <div class="block-header">
                    <h2 class="align-center">
                        PERSONNEL MANAGEMENT
                    </h2>
                </div>            

                <div class="row clearfix">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                        <div class="card">
                            <div class="header">
                                <button type="button" class="btn bg-blue waves-effect" data-toggle="modal" data-target="#addPersonnelModal">
                                    <i class="material-icons">add</i>
                                    <span>ADD PERSONNEL</span>
                                </button>
                                <input type="text" id="txtSearchPerKey" name="txtSearchPerKey">
                                <span id="spanSearchPerKey">Search: </span>
                            </div>
                            <div class="body">
                                <div class="table-responsive">
                                    <table id="tblViewPersonnels" class="table table-bordered table-striped table-hover">
                                        <thead>
                                            <tr>
                                                <th class="align-center">Name</th>
                                                <th class="align-center">Position</th>
                                                <th class="align-center">Action</th>
                                            </tr>
                                        </thead>
                                        <tfoot>
                                            <tr>
                                                <th class="align-center">Name</th>
                                                <th class="align-center">Position</th>
                                                <th class="align-center">Action</th>
                                            </tr>
                                        </tfoot>
                                        <tbody>
                                            <!-- code will be generated here -->
                                        </tbody>
                                    </table>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    else
        <script>window.location = "/auth";</script>
    @endif
@endsection

web.php - 这是路线

Auth::routes();

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

登录

Route::get('/auth', 'AuthController@index');
Route::post('/auth/checklogin', 'AuthController@checklogin');
Route::get('/auth/logout', 'AuthController@logout');

人员

Route::get('/personnel', function () {
    return view('personnel');
});

标签: phplaravel

解决方案


因为您使用的是username列而不是 laravel 的默认email列,

尝试这样做:

if(Auth::attempt(['username' => $request['username'], 'password' => $request['password']])) {
   return redirect('personnel');
}else {
    return back()->with('error', 'Wrong Login Details');
}

推荐阅读