简体   繁体   English

Auth ::尝试在Laravel 5中不起作用

[英]Auth::attempt not working in Laravel 5

I've read dozens of similar posts, still cant't make it work. 我已经阅读了数十篇类似的文章,但仍然无法使其正常工作。 I always get the 'login failed' message. 我总是收到“登录失败”消息。 This is my users table 这是我的用户表

CREATE TABLE users (
id int(10) unsigned NOT NULL,
first_name varchar(255) COLLATE utf8_unicode_ci NOT NULL,
last_name varchar(255) COLLATE utf8_unicode_ci NOT NULL,
slug varchar(255) COLLATE utf8_unicode_ci NOT NULL,
email varchar(255) COLLATE utf8_unicode_ci NOT NULL,
password varchar(255) COLLATE utf8_unicode_ci NOT NULL,
remember_token varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
created_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
updated_at timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

See password field is 255, so large enough for hashing. 请参阅密码字段为255,足够大以进行哈希处理。

Here is my App\\Model\\User class 这是我的App \\ Model \\ User类

namespace App\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    protected $fillable = ['name', 'email', 'password'];
    protected $hidden = ['password', 'remember_token'];
}

This is the login controller 这是登录控制器

namespace App\Http\Controllers\Backend;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class MainController extends Controller
{
    public function getLogin()
    {
        return view('backend.login');
    }

    public function postLogin(Request $request)
    {
        if (Auth::attempt([
            'email'    => $request->input('email'),
            'password' => $request->input('password')
        ])) {
            echo 'login successful'; exit();
        }
        else {
            echo 'login failed'; exit();
        }
    }
}

User records in db were inserted with their password set to 插入db中的用户记录,密码设置为

Crypt::encrypt('123456')

This gave me nearly 200 chaacters long hashed passwords stored in the db (is that normal?) 这给了我将近200个角色存储在数据库中的长哈希密码(是正常的吗?)

My config.app file says 我的config.app文件说

'key' => env('APP_KEY'),
'cipher' => 'AES-256-CBC',

And finally here is my config.auth file 最后是我的config.auth文件

return [
    'defaults' => [
        'guard' => 'web',
        'passwords' => 'users',
    ],
    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'users',
        ],
        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Model\User::class,
        ],
    ],
    'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'password_resets',
            'expire' => 60,
        ],
    ],
];

I tried to dump $request->input('email')/$request->input('password') from the form, and everything is as expected. 我试图从表单中转储$ request-> input('email')/ $ request-> input('password'),并且一切都按预期进行。 I tried to dump the query log and the query seems to be right ('select * from users where email = ? limit 1, with correct email field binding). 我试图转储查询日志,查询似乎是正确的(从电子邮件=?限制为1的用户中选择*,并且电子邮件字段绑定正确)。

What else can I check? 我还能检查什么? Is there a way to "deeply inspect" Auth::attempt to check what's going wrong over there? 有没有一种方法可以“深入检查” Auth ::试图检查那里出了什么问题?

Any help would be appreciated. 任何帮助,将不胜感激。

The issue here is you are using Crypt::encrypt('123456') to set user password. 这里的问题是您正在使用Crypt::encrypt('123456')设置用户密码。 Crypt is not used for this purpose as it requires Decrypt to work which is not implemented in Auth class Crypt不用于此目的,因为它需要Decrypt才能工作,这在Auth类中未实现

Simpy use Hash to hash password before you save to database Simpy在保存到数据库之前使用Hash来哈希密码

Example: 例:

User::create([ 
    'password' => Hash::make('123456'),
    'username' => 'hitman'
]);

You can also do hashing in user model by adding this to User model class: 您还可以通过将其添加到User模型类中来在用户模型中进行哈希处理:

public function setPasswordAttribute($password)
{
    $salt = md5(str_random(64) . time());
    $this->attributes['password'] = \Hash::make($password);
    $this->attributes['salt'] = $salt;
}

And later you can simply do this: 然后您可以简单地执行以下操作:

User::create([ 
    'password' => '123456', //hashing will be done in model
    'username' => 'hitman'
]);

希望这对你有帮助

Hash::make($data['password'])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM