我是新来的拉拉。我的登录控件有问题,无法匹配来自表单和数据库的数据。
错误消息说:
DecryptException在BaseEncrypter.php第45行中:有效负载无效。
控制器
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>发布于 2015-11-10 11:42:05
您正在解密未加密的字符串(这就是您获得错误的原因):
$pass = Crypt::decrypt($request->password);为什么在这里?$pass什么也不做。
到目前为止,您正在搜索一个具有未加密密码的用户。
要检查密码是否有效,请执行而不是对DB中的密码进行解密,对插入的密码进行加密,并查看它们是否匹配。
插入密码时如何加密密码?
在默认情况下,Laravel提供了一个大部分完成的身份验证,如果您不知道自己在做什么,并且希望具有尽可能安全的身份验证,则应该使用它。在这里阅读:http://laravel.com/docs/5.0/authentication
要编写自己的身份验证,应该使用哈希类:
$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...
}https://stackoverflow.com/questions/33629089
复制相似问题