Search code examples
phpauthenticationlaravel-4laravel-routingbefore-filter

Authentication laravel 4 on beforeFilter('auth') not work


i have some controller that i want to secure this from login page and i add login page and im my controllers i add __construct to secure this

see my code :

in NewsController i want secure getindex page from Authentication

class NewsController extends Basecontroller{

    public function __construct() {
   $this->beforeFilter('csrf', array('on'=>'post'));
   $this->beforeFilter('auth');
}

and in route file i add below code :

Route::post('login',function()
{

 //print_r(Hash::make(Input::get('UserName')));

 if (Auth::attempt(array('UserName'=>Input::get('UserName'), 'password'=>Input::get('Password')))) {
  return Redirect::to('news')->with('message', 'You are now logged in!');
  } else {
  return Redirect::to('login')
    ->with('message', 'Your username/password combination was incorrect')
    ->withInput();
}

I'm sure that user pass is corect and passed but in controller redirect me in login page

what is wrong ?

my complete route.php file

Route::get('login',function(){
 return View::make('Admin/Login');

 });
Route::post('login',function()
{


if (Auth::attempt(array('UserName'=>Input::get('UserName'),     'password'=>Input::get('Password')))) {
    return Redirect::to('news')->with('message', 'You are now logged in!');
} else {
return Redirect::to('login')
    ->with('message', 'Your username/password combination was incorrect')
    ->withInput();
}

});


Route::controller('news', 'NewsController');
Route::controller('article', 'ArticleController');
Route::controller('albume', 'AlbumeController');
Route::controller('staticcontent', 'StaticContentController');
Route::controller('links', 'LinksController');
Route::controller('faq', 'FAQController');
Route::controller('employment', 'EmploymentController');
Route::controller('poll', 'PollController');
Route::controller('branches', 'BranchesController');
Route::controller('user', 'UserController');
Route::controller('access', 'AccessLevelController');
Route::controller('metatag', 'MetaTagController');
Route::controller('product', 'ProductController');

and get login view :

{{Form::open(array('method'=>'post','id'=>'Filter'))}}
<table class="Login">
<tr>
<td>user</td>
<td>{{Form::text('UserName')}}</td>
</tr>
<tr>
<td>pass</td>
<td>{{Form::password('Password')}}</td>
</tr>
<tr>
<td colspan="2">{{HTML::link('','forget pass ?')}}</td>
</tr>
<tr>
<td colspan="2">{{Form::submit('login')}}</td>
</tr>
</table>
{{Form::close()}}

Solution

  • i found answer

    i want add user table ID into Model User.php

    protected $primaryKey = "ID";