Laravel 身份验证失败
Laravel Authentification fails
我遇到了问题,当我在登录表单中输入正确的电子邮件和密码时,我的身份验证一直转到 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'
));
}
}
取自 Laravel Authentication Documentation:
Remember: when building the database schema for this model, make the password column at least 60 characters. Also, before getting started, make sure that your users (or equivalent) table contains a nullable, string remember_token column of 100 characters.
为了将来参考,Laravel 附带了 users
table 的迁移,您可以在 database/migrations/2014_10_12_000000_create_users_table.php
中找到它。您可以使用它通过 运行 php artisan migrate
生成 users
table。
除了此处定义的默认列之外,您当然可以添加其他列,请阅读 Schema Builder Documentation 了解更多信息。
我遇到了问题,当我在登录表单中输入正确的电子邮件和密码时,我的身份验证一直转到 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'
));
}
}
取自 Laravel Authentication Documentation:
Remember: when building the database schema for this model, make the password column at least 60 characters. Also, before getting started, make sure that your users (or equivalent) table contains a nullable, string remember_token column of 100 characters.
为了将来参考,Laravel 附带了 users
table 的迁移,您可以在 database/migrations/2014_10_12_000000_create_users_table.php
中找到它。您可以使用它通过 运行 php artisan migrate
生成 users
table。
除了此处定义的默认列之外,您当然可以添加其他列,请阅读 Schema Builder Documentation 了解更多信息。