Auth ::尝试不在Laravel 5中工作

时间:2016-02-07 15:16:27

标签: php authentication laravel-5

我已经阅读过几十篇类似的帖子,仍然无法使其发挥作用。我总是收到'登录失败'的消息。 这是我的用户表

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;

请参阅密码字段为255,足以进行散列。

这是我的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'];
}

这是登录控制器

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();
        }
    }
}

插入了db中的用户记录,其密码设置为

Crypt::encrypt('123456')

这给了我近200个存储在db中的chaachters长哈希密码(这是正常的吗?)

我的config.app文件说

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

最后这是我的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,
        ],
    ],
];

我尝试从表单转储$ request-> input('email')/ $ request-> input('password'),一切都按预期进行。 我试图转储查询日志,并且查询似乎是正确的('select * from users where email =?limit 1,with correct field field binding。)

我还能检查什么? 有没有办法“深入检查”Auth ::尝试检查那里出了什么问题?

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:2)

此处的问题是您使用<form method="post" action="{{ path('aiem_platform_add_offer')}}"> <div class="form-group"> <label for="title">Title</label> <input type="text" class="form-control" id="title" name="title" placeholder="Title"> </div> <div class="form-group"> <input type="file" class="form-control" id="myFile" name="myFile"> </div> <div class="form-group"> <label for="contenu">Contenu</label> <textarea class="form-control ckeditor" id="body" name="body"></textarea> </div> <button type="submit" class="btn btn-default">Add</button> </form> 来设置用户密码。 Crypt::encrypt('123456')未用于此目的,因为它需要Crypt才能使用Decrypt

在保存到数据库

之前,先使用Auth哈希密码

示例:

Hash

您还可以在用户模型中进行散列,方法是将其添加到User::create([ 'password' => Hash::make('123456'), 'username' => 'hitman' ]); 模型类:

User

之后你可以简单地这样做:

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

答案 1 :(得分:0)

我希望这对您有帮助

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