首页 文章

Laravel Auth尝试失败

提问于
浏览
2

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

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

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

  • 我的用户模型实现了UserInterface和Remindable Interface .

  • 我的密码在创建到数据库时被哈希 .

  • 我的数据库中的密码字段是varchar 100,这应该足以散列密码 .

  • 我正在记录的用户已在数据库中创建并激活 .

非常感谢您的任何见解 .

User Model

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

}

Account Controller

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

Routes

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'

3 回答

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

    清理代码并修复问题 .

  • 6

    你的代码看起来对我来说,所以你必须检查一些事情:

    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

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

    protected $primaryKey = 'user_id';
    

相关问题