我正在使用Larave 4.2,一切正常。如果我输入正确的凭据,我将被带到正确的URL(带有身份验证过滤器的URL)。但是我目前遇到的问题是,当输入的字段之一不正确并且用户提交它将显示白屏时。
我当然希望用户将被重定向回带有输入的登录页面并显示错误。
我已经检查了过滤器,很确定它仍然是 Laravel 附带的,没有改变任何东西。
我的路线
<?php
Route::get('login', function()
{
// just a shortcut to redirec to /login into /cms/login : prevents redirect LOOP
return Redirect::route('cms.login');
});
Route::group(array('prefix' => 'cms'), function()
{
Route::get('/', function()
{
if (Auth::guest())
{
return Redirect::route('cms.login');
}
else
{
return Redirect::route('cms.home');
}
});
Route::get('login', array(
'as' => 'cms.login',
'uses' => 'CMSController@login'
));
Route::post('login', array(
'as' => 'cms.postLogin',
'uses' => 'CMSController@userLogin'
));
Route::get('logout', array(
'as' => 'cms.logout',
'uses' => 'CMSController@userLogout'
));
Route::group(array('before' => 'auth'), function()
{
Route::get('home', array(
'as' => 'cms.home',
'uses' => 'CMSController@home'
));
Route::get('my-account', array(
'as' => 'cms.myaccount',
'uses' => 'AccountsController@myAccount'
));
Route::get('my-account/edit', array(
'as' => 'cms.edit-myaccount',
'uses' => 'AccountsController@editMyAccount'
));
Route::resource('accounts', 'AccountsController');
Route::resource('products', 'ProductsController');
Route::resource('news', 'NewsController');
Route::resource('settings', 'SettingsController');
Route::resource('homepage-sliders', 'HomepageSlidersController');
Route::resource('testimonials', 'TestimonialsController');
Route::resource('effects', 'EffectsController');
});
});
用户模型
<?php
use IlluminateAuthUserTrait;
use IlluminateAuthUserInterface;
use IlluminateAuthRemindersRemindableTrait;
use IlluminateAuthRemindersRemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* Fillable array
*
*/
protected $fillable = array('email', 'password', 'username', 'position', 'mobile');
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = array('password', 'remember_token');
/**
* Sets the Validation Rules when Logging In
*
* @var array
*/
public static $loginRules = array(
'email' => 'required|email',
'password' => 'required|alpha_dash|min:6'
);
/**
* Sets the Validation Rules creating a User
*
* @var array
*/
public static $rules = array(
'email' => 'required|email|unique:users',
'username' => 'required|min:2|unique:users',
'position' => 'required|',
'mobile-number' => 'required|numeric|digits:11',
'password' => 'required|alpha_dash|min:6|confirmed',
'password_confirmation' => 'required|alpha_dash|min:6'
);
/**
* Sets the Validation Rules updating a User
*
* @var array
*/
public static $updateRules = array(
'username' => 'required|min:2',
'password' => 'required|alpha_dash|min:6|confirmed',
'password_confirmation' => 'required|alpha_dash|min:6'
);
/**
* Defines many-to-many relationship with Module
*
*/
public function permissions()
{
return $this->belongsToMany('Module', 'permissions')->withPivot('add','edit', 'view','delete');
}
/**
* 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;
}
/**
* Gets the Remember Token
*
* @return string $this->remember_token
*/
public function getRememberToken()
{
return $this->remember_token;
}
/**
* Set the Remember Token
*
* @param string $value
*/
public function setRememberToken($value)
{
$this->remember_token = $value;
}
/**
* Get the Remember Token name
*
* @return string 'remember_token'
*/
public function getRememberTokenName()
{
return 'remember_token';
}
/**
* Get the password and Hash it before saving to the database.
*
* @param string $value
*/
public function setPasswordAttribute($value)
{
$this->attributes['password'] = Hash::make($value);
}
/**
* Checks if Guest User input invalid credentials
*
* @param array $credentials
* @return object $validation
*/
public static function loginIsInvalid($credentials)
{
$validation = Validator::make($credentials, self::$loginRules);
if ($validation->fails())
{
return $validation;
}
}
我的CMS主计长
<?php
class CMSController extends BaseController {
/**
* Display the login page.
* GET /cms
*
* @return Response
*/
public function login()
{
return View::make('cms.login');
}
/**
* Accepts the post request for login
* of user in CMS
*
*/
public function userLogin()
{
$user_credentials['email'] = Input::get('email');
$user_credentials['password'] = Input::get('password');
//sets the remember_me variable
if (Input::has('remember'))
{
$remember_me = true;
}
else
{
$remember_me = false;
}
if ($errors = User::loginIsInvalid($user_credentials))
{
return Redirect::route('cms.login')->withInput()->withErrors($errors);
}
if (Auth::attempt(array(
'email' => $user_credentials['email'],
'password' => $user_credentials['password']), $remember_me))
{
return Redirect::route('cms.home');
}
}
/**
* Accepts the post request for logout
* of user in CMS
*
*/
public function userLogout()
{
Session::clear();
Auth::logout();
return Redirect::route('cms.login');
}
/**
* Directs user to home page
*
*/
public function home()
{
return View::make('cms.home');
}
}
目前在您的代码中,Auth::attempt()
后没有任何内容 - 所以如果身份验证失败 - 它无处可去。
只需在Auth::attempt()
后添加退货即可使其正常工作
if (Auth::attempt(array(
'email' => $user_credentials['email'],
'password' => $user_credentials['password']), $remember_me))
{
return Redirect::route('cms.home');
}
return Redirect::route('cms.login')->withInput()->withErrors($errors);