Laravel 5.1登录控制器工作不正常


Laravel 5.1 Login Controller Doesnt Work Well

我是laravel的新手。我的登录控制有问题,无法匹配表单和数据库中的数据。

错误信息显示:

BaseEncrypter.php第45行中的DecryptException:有效负载无效。

控制器

namespace App'Http'Controllers;
use DB;
use Hash;
use Crypt;
use Validator;
use Illuminate'Http'Request;
use App'User;
use App'Http'Requests;
use App'Http'Controllers'Controller;

class LoginControl extends Controller
{
    public function login(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'email' => 'required',
            'password' => 'required',
        ]);
        if ($validator->fails()) {
            return redirect('/login')
                        ->withErrors($validator)
                        ->withInput();
        }
        else{
            $email = $request->email;
            $password = $request->password;
            $results = DB::select('select * from users where email = ? and password = ?', [$email,$password]);
            $pass = Crypt::decrypt($request->password);
            if($results == NULL){
                return redirect('/login');
            }
            else{
                return redirect('/');
            }
        }
    }
}

路由器

<?php
Route::get('/', function () {
    return view('welcome');
});
Route::get('/register', function () {
    return View::make('register');
});
Route::get('/login', function() {
    return View::make('login');
});
Route::post('actionregis', 'RegisControl@store');
Route::post('actionlogin', 'LoginControl@login');

查看

<html lang="en">
    <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
        <title>Laravel Quickstart - Basic</title>
        <!-- CSS And JavaScript -->
    </head>
    <body>
        <div class="container">
            <nav class="navbar navbar-default">
                <!-- Navbar Contents -->
            </nav>
        </div>
        <div class="panel-body">
        <div class="col-md-8 col-md-offset-2">
        <!-- New Task Form -->
        <form action="/testing/public/actionlogin" method="POST" class="form-horizontal">
            {{ csrf_field() }}
            @if (count($errors) > 0)
                <!-- Form Error List -->
                <div class="alert alert-danger">
                    <strong>Whoops! Something went wrong!</strong>
                    <br><br>
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif  
            <!-- Task Name -->
                {!! csrf_field() !!}
                <div class="form-group">
                    <label for="user">Email</label>
                    <input type="text" name="email" id="task-email" class="form-control">
                </div>
                <div class="form-group">
                    <label for="user">Password</label>
                    <input type="password" name="password" id="task-password" class="form-control">
                </div>
                <div class="form-group">
                    <input type="checkbox" name="remember"> Remember Me
                </div>
                <div class="form-group">
                    <button type="submit" class="btn btn-default">Login</button>
                </div>
            </form>
        </div>
    </div>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    </body>
</html>

您正在解密一个未加密的字符串(这就是您收到错误的原因):

$pass = Crypt::decrypt($request->password);

为什么这甚至在这里?$pass没有做任何事情。

截至目前,您正在搜索具有未加密密码的用户。

要检查密码是否有效,请执行NOT解密数据库中的密码,加密插入的密码并查看它们是否匹配。

插入密码时如何加密?

默认情况下,Laravel提供了一个基本完成的身份验证,如果你不知道自己在做什么,并且希望身份验证尽可能安全,你应该使用它。点击此处阅读:http://laravel.com/docs/5.0/authentication

要编写自己的身份验证,您应该使用Hash类:

$password = Hash::make('some_password'); // Use this to hash your password (you can store that in the DB)
// To check the password, you parse the user's hashed password from the DB
... // <- parse user here
$hashedPassword = $user->password;
// Password checking
if (Hash::check('some_password', $hashedPassword))
{
    // The passwords match...
}

由于您有很多数据,可能数据库中的列类型太小,因此会出现此错误。

在迁移中,尝试将列设置为长文本,而不是文本,看看这是否有效。