Laravel Auth尝试失败


Laravel Auth attempt failing

在我把它们带到这里之前,我真的试着自己调试我的问题,但是我真的找不到解决我的laravel认证问题的方法,尽管它似乎是一个常见的问题。

我的身份验证无法登录。它总是返回false,我不明白为什么。

我已经阅读了这里的其他一些问题,他们的解决方案并没有解决我的特殊情况。

  • My User模型实现了UserInterface和Remindable Interface。
  • 我的密码在创建到数据库时被散列。
  • 我的密码字段在我的数据库是varchar 100,这应该是足够的哈希密码。
  • 我要登录的用户已经在数据库中创建并激活。

非常感谢你的建议。

用户模型

<?php
use Illuminate'Auth'UserInterface;
use Illuminate'Auth'Reminders'RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
    protected $fillable = array('email', 'username', 'password', 'password_temp', 'code', 'active');
    public $timestamps = false; 
    protected $softDelete = false;
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'Users';
    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = 'password';
    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }
    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }
    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }
}

帐户控制器

class AccountController extends BaseController {
public function getLogin() {
    return View::make('account.login');
}
public function postLogin() {
    $validator = Validator::make(Input::all(),
        array(
            'email' => 'required',
            'password' => 'required'
        )
    );
    if($validator->fails()) {
        return Redirect::route('login')
                    ->withErrors($validator);
    } else {
        $auth = Auth::attempt(array(
                'email' => Input::get('email'),
                'password' => Input::get('password'),
                'active' => 1
                ));
            if($auth) {
                return Redirect::route('Create-Account');
            }
        }
        return Redirect::route('login')
                    ->with('global', 'There was a problem logging you in. Please check your credentials and try again.');
}
public function getCreate() {
    return View::make('account.create');
}
public function getviewReturn() {   
    return View::make('account.return');
}
public function postCreate() {
    $validator = Validator::make(Input::all(),
        array(
            'email' => 'required|max:50|email|unique:Users',
            'username' => 'required|max:15|min:4|unique:Users',
            'password' => 'required|min:6',
            'password2' => 'required|same:password'
        )
    );
    if ($validator->fails()) {
        return Redirect::route('Post-Create-Account')
                    ->withErrors($validator)
                    ->withInput();
    }
    else {
        $email = Input::get('email');
        $username = Input::get('username');
        $password = Input::get('email');
        $code = str_random(60);
        $user = User::create(array(
            'email' => $email,
            'username' => $username,
            'password' => Hash::make($password),
            'code' => $code,
            'active' => 0));
});
return Redirect::to('account/return')

路线

Route::group(array('before' => 'guest'), function() {
Route::group(array('before' => 'csrf'), function() {
    Route::post('/account/create', array(
        'as' => 'Post-Create-Account',
        'uses' => 'AccountController@postCreate'
    ));

    Route::post('/account/login', array( 
        'as' => 'postlogin', 
        'uses' => 'AccountController@postLogin'
    ));

});
    Route::get('/account/login', array(
        'as' => 'login',
        'uses' => 'AccountController@getLogin'
));
Route::get('/account/create', array(
    'as' => 'Create-Account',
    'uses' => 'AccountController@getCreate'
));
Route::get('/account/activate/{code}', array(
    'as' => 'Activate-Account',
    'uses' => 'AccountController@getActivate'

创建用户时,您已完成

$password = Input::get('email');

应该是

$password = Input::get('password');

所以如果你尝试用"email"作为密码登录-它会工作!:)

所以如果你改变这个

else {
        $email = Input::get('email');
        $username = Input::get('username');
        $password = Input::get('email');
        $code = str_random(60);
        $user = User::create(array(
            'email' => $email,
            'username' => $username,
            'password' => Hash::make($password),
            'code' => $code,
            'active' => 0));
});

else {
        $user = User::create(array(
            'email' => Input::get('email'),
            'username' => Input::get('username'),
            'password' => Hash::make(Input::get('password');),
            'code' => str_random(60),
            'active' => 0));
});

清理代码并修复问题

你的代码在我看来是正确的,所以你必须检查一些东西:

1)手动尝试对你有用吗?

dd( Auth::attempt(['email' => 'youremail', 'password' => 'passw0rt']) );

2)用户哈希值手动检查?

$user = User::find(1);
var_dump( Hash::check($user->password, 'passw0rt') );
dd( Hash::check($user->password, Input::get('password')) );

尝试在用户模型中添加primaryKey字段。应该是这样的:

protected $primaryKey = 'user_id';

我认为是Apache版本问题。你需要更新Apache2.4.