Laravel登录始终返回false

时间:2018-10-14 18:16:02

标签: php laravel

我正在尝试创建一个登录系统,但是它总是返回错误的详细信息,即使在我的数据库中存在我的文本的电子邮件和密码,我也可以在phpMyAdmin中看到它们。我知道PHP Artisan Auth的存在,但我必须只编写一次代码。

我在控制器checkLogin(),successLogin()(也许应该删除它,因为它可能毫无意义)和logout()中创建了3种方法。

我坚信我的问题出在路线上。请注意,所有文件,即index.blade.php和login.blade.php位于以下位置:views/users/

<?php

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

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

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

function successLogin()
{
    return view('/users');
}

function logout()
{
    Auth::logout();

    return redirect('users.index');
}

刀片

@extends('master')

@section('content')
    <style type="text/css">
        .box {
            width: 600px;
            margin: 0 auto;
            border: 1px solid #ccc;
        }
    </style>
    <div class="container box">
        <h3 align="center">Please Login To Continue</h3><br/>
        @if(isset(Auth::user()->email))
            <script>window.location = "/users";</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('/login/checklogin') }}">
            {{ csrf_field() }}
            <div class="form-group">
                <label>Enter Email</label>
                <input type="email" name="email" class="form-control"/>
            </div>
            <div class="form-group">
                <label>Enter Password</label>
                <input type="password" name="password" class="form-control"/>
            </div>
            <div class="form-group">
                <input type="submit" name="login" class="btn btn-primary" value="Login"/>
            </div>
        </form>
    </div>
@endsection

路线

Route::post('/login/checklogin', 'UserController@checklogin');
Route::get('/login/successlogin', 'UserController@successlogin');
Route::get('/login/logout', 'UserController@logout');
Route::get('/login',function () {
    return view('/users/login');
});

0 个答案:

没有答案