Laravel Authentification失败了

时间:2015-05-29 10:13:16

标签: authentication laravel login controller

我遇到了问题,我的Authentifications一直在查看else语句,而我在登录表单中输入了正确的电子邮件和密码。使用else语句我的意思是SessionController的最后一行

    else 

    {
       return Redirect::to('login')
    }

这是我的routes.php

// ~ Root
Route::get('/', array('as' => 'root', 'uses' => 'PageController@showIndex'));

// ~ Session ~ Login ~ Logout
Route::get('login', array('as' => 'newSession', 'uses' => 'SessionController@newSession'));
Route::post('login', array('as' => 'setSession', 'uses' => 'SessionController@setSession'));
Route::get('logout', array('as' => 'destroySession', 'uses' => 'SessionController@destroySession'));

这是我的SessionController

<?php

class SessionController extends BaseController {

    public function newSession() {

        $this->layout->content = View::make('login');

    }

    public function setSession() {

        $rules = array(
            'email'    => 'required|email',
            'password' => 'required|alphaNum|min:3'
        );

        $validator = Validator::make(Input::all(), $rules);

        if ($validator->fails()) {
            return Redirect::to('login')
                ->withErrors($validator)
                ->withInput(Input::except('password'));
        } else {

            // create our user data for the authentication
            $userdata = array(
                'email'     => Input::get('email'),
                'password'  => Input::get('password')
            );

            // attempt to do the login
            if (Auth::attempt($userdata)) {

                return Redirect::to('asd');

            } else {
                return Redirect::to('login')
                ->withErrors($validator)
                ->withInput(Input::except('password'));
                // validation not successful, send back to form
                // return Redirect::to('login')->withErrors($validator);
            }
        }
    }

    public function destroySession() {

        Auth::logout();
        return Redirect::to('login')->with('message', 'You are logged out'); 

    }
}

这是我的表格

{{ Form::open(array('action' => 'SessionController@setSession', 'method' => 'POST', 'class' => 'form-horizontal navbar-form navbar-right')) }}
<div id="navbar" class="navbar-collapse collapse">
    <div class="form-group">
        <p>
            {{ $errors->first('email') }}
            {{ $errors->first('password') }}
        </p>
        <p>
            {{ Form::label('email', 'Email Address', array('class' => 'navLogTxt')) }}          
            {{ Form::text('email', Input::old('email'), array('class' => 'form-control')) }}      
            {{ Form::label('password', 'Passwort', array('class' => 'navLogTxt')) }}            
            {{ Form::password('password', array('class' => 'form-control')) }}
            {{ Form::submit('Anmelden!', array('class' => 'btn btn-success')) }}
        </p>                        
    </div>
</div>
{{ Form::close() }}

最后这是我的播种机

<?php

class UserTableSeeder extends Seeder {

    public function run() {

        DB::table('users')->delete();
        User::create(array(
            'email'    => 'test@mail.de',
            'password' => Hash::make('awesome')
        ));
        User::create(array(
            'email'    => 'test@mail2.de',
            'password' => 'test'
        ));

    }

}

1 个答案:

答案 0 :(得分:0)

取自Laravel Authentication Documentation

  

请记住:在为此模型构建数据库架构时,使密码列至少为60个字符。此外,在开始之前,请确保您的用户(或等效的)表包含一个可为空的字符串remember_token列,其中包含100个字符。

为了将来参考,Laravel附带users表的迁移,您可以在database/migrations/2014_10_12_000000_create_users_table.php中找到该表。您可以通过运行users来使用它来生成php artisan migrate表。

您当然可以添加除此处定义的默认列之外的其他列,只需阅读Schema Builder Documentation以获取更多信息。