Laravel Auth尝试失败

时间:2014-05-17 12:45:35

标签: php authentication login laravel

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

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

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

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

4 个答案:

答案 0 :(得分:6)

创建用户时已完成

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

应该是

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

所以,如果您尝试使用&#34;电子邮件&#34;作为密码 - 它会工作! :)

所以如果你改变这个

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 :(得分: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')) );

答案 2 :(得分:1)

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

protected $primaryKey = 'user_id';

答案 3 :(得分:0)

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