【问题标题】:Laravel Auth attempt failingLaravel 身份验证尝试失败
【发布时间】:2014-07-05 20:46:05
【问题描述】:

在我把问题带到这里之前,我真的尝试自己调试我的问题,但我真的找不到我的 laravel 身份验证问题的解决方案,尽管这似乎是一个常见问题。

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

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

  • 我的用户模型实现了 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'

【问题讨论】:

    标签: php authentication login laravel


    【解决方案1】:

    创建用户时你已经完成

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

    应该是

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

    因此,如果您尝试使用“电子邮件”作为密码登录 - 它会起作用! :)

    所以如果你改变这个

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

    清理您的代码并解决问题。

    【讨论】:

    • 哇,这只是我的一个愚蠢的错误!这绝对解决了一切!也感谢代码清理,我对此很陌生,想尽我所能学习! :)
    【解决方案2】:

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

    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')) );
    

    【讨论】:

    • Hash::check() 的参数应该交换。它的 Hash::check('passw0rt',$user->password);
    • 你写错了Hash::check('plain-text', $hashedPassword)
    【解决方案3】:

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

    protected $primaryKey = 'user_id';
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 2014-05-29
        • 1970-01-01
        • 2014-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多