php - Laravel Auth::attempt() 总是假的?

标签 php authentication laravel

我最近开始学习 Laravel(PHP MVC 框架),并且在使用 Auth::attempt($credentials) 对用户进行身份验证时遇到了很多问题。

我将把我的代码片段放在下面。在我的 mySQL 数据库中,我有一个包含以下键=>值的记录: id=>1, username=>'admin', password=>'admin', created_at=>0000-00-00 00:00:00, updated_at=>0000-00-00 00:00:00

Validator 通过,但 Auth:attempt 总是失败。

如果下面的代码和我的小解释还不够,请告诉我! :-)

路由.php:

Route::get('login', 'AuthController@showLogin');
Route::post('login', 'AuthController@postLogin');

授权 Controller :

public function showLogin(){

    if(Auth::check()){
        //Redirect to their home page
    }
    //Not logged in
    return View::make('auth/login');
}

public function postLogin(){

    $userData = array(
        'username' => Input::get('username'),
        'password' => Input::get('password')
    );

    $reqs = array(
        'username' => 'Required',
        'password' => 'Required'
    );

    $validator = Validator::make($userData, $reqs);

    if($validator->passes() && Auth::attempt($userData)){
        return Redirect::to('home');
    }
    return Redirect::to('login')->withInput(Input::except('password'));
}

最佳答案

我相信 attempt() 方法会对发送的密码进行哈希处理。鉴于您的数据库条目的密码是明文形式的“admin”,这将失败。用 Hash::make($password); 保存你的密码;我相信你的问题应该得到解决。

关于php - Laravel Auth::attempt() 总是假的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18006597/

相关文章:

html - 登录表单已更改,用户名已更改为电子邮件,自动完成功能不断输入已保存的电子邮件

php - 我如何使用 php 和 dom 获得第一个、第二个和第三个 TD 值

php - @include 如何在 laravel 中工作?

android - 检查登录名和密码是否已在首选项中

ios - ADAL iOS 静默登录抛出错误

php - Laravel 阻止用户编辑/查看其他用户的资源

php - 如何结合 laravel 和 vue 路由

php - Laravel Livewire 文件上传返回 Null

PHP 邮件 $email = $_POST ['email' ] 在 $recipients 中

php - 在没有 HTTPS 的情况下保护服务器到服务器 PHP 通信