Laravel Auth :: attempt()失败

时间:2014-08-18 14:27:35

标签: php laravel login laravel-4

我正在使用Laravel 4登录系统。我可以成功register用户,但当我尝试login使用详细信息时 - 电子邮件和密码每次都会失败,我不知道为什么。

这是Schemausers表的db

Schema::create('users', function($table){ 
            $table->increments('id'); 
            $table->string('fname', 255); 
            $table->string('lname', 255); 
            $table->string('email')->unique(); 
            $table->string('password',128); 
            $table->string('remember_token', 100);
            $table->timestamps(); 
        }); 

这是我的Login Form

<form action="{{action('HomeController@handleLogin')}}" method="post">
        <div class="form-group">
            <label for="email">Email</label>
            <input type="text" class="form-control" name="email"/>
        </div>
        <div class="form-group">
            <label for="password">Password</label>
            <input type="password" class="form-control" name="password"/>
        </div>
        <input type="submit" value="Login" class="btn btn-primary"/>
    </form>

这是我Login Action中的HomeController

public function handleLogin() {

        $rules = array(
            'email'    => 'required', 
            'password' => 'required'
        );

        $credentials = array(
            'email' => Input::get('email'),
            'password' => Input::get('password')
        );

        $validator = Validator::make($credentials, $rules);
        if($validator->passes()){
            echo "Validation passed";
            if(Auth::attempt($credentials)){
                echo "Logged In";die();
            }else{
                echo "Logged Failed";die();
            }
        }else{
            echo "Validation failed";die();
        }
    }

我不明白它失败的原因,即使我将电子邮件和密码硬编码到$credentials数组中也无法登录。

任何帮助都非常感谢。

注册代码

public function handleRegister(){

        //Validation Rules
        $rules = array(
            'fname'=>'required|alpha|min:2',
            'lname'=>'required|alpha|min:2',
            'email'=>'required|email|unique:users',
            'password'=>'required|alpha_num|between:6,12|confirmed',
            'password_confirmation'=>'required|alpha_num|between:6,12'
        );

        //Validator
        $validator = Validator::make(Input::all(), $rules);
        if($validator->passes()){//PASS
            //Save user to DB
            $user = new User;
            $user->fname = Input::get('fname');
            $user->lname = Input::get('lname');
            $user->email = Input::get('email');
            $user->password = Hash::make(Input::get('password'));
            $user->save();
            return Redirect::action('HomeController@getLogin')->with('message', 'Thanks for registering!');
        }else{//FAIL
            //Display Errors
            return Redirect::action('HomeController@getRegister')->with('message', 'The following errors occured')->withErrors($validator)->withInput();
        }

    }

Database row

id  type    fname   lname   email   password    school  address_1   address_2   address_3   address_4   remember_token  created_at  updated_at
7   \N  Jack    Coldrick    jackcoldrick@yahoo.com  $2y$10$nRdbkOeEyOB4NLiphGEb5u2Qvcs2Xwu/x7zIcxq3mY3TQWCkiE6a6    \N  \N  \N  \N  \N      2014-08-18 14:01:22 2014-08-18 14:01:22

1 个答案:

答案 0 :(得分:1)

尝试手动登录

public function handleLogin() {

        $rules = array(
            'email'    => 'required', 
            'password' => 'required'
        );

        $credentials = array(
            'email' => Input::get('email'),
            'password' => Input::get('password')
        );

        $validator = Validator::make($credentials, $rules);
        if($validator->passes()){
            echo "Validation passed";
            $user = User::where('email', $credentials['email'])
            ->andWhere('password', Hash::make($credentials['email']))
            ->first();

            if(null !== $user && Auth::login($user)){
                echo "Logged In";die();
            }else{
                echo "Logged Failed";die();
            }
        }else{
            echo "Validation failed";die();
        }
    }