title: Laravel Authentication
body: <pre><code>$this-&gt;beforeFilter(function()
          {
               Config::set('auth.model', 'User');
               if ((Auth::guest())) {
              //dd(Auth::guest());
              $msg3  = "Please Login First";
              // dd("ok");
              return Redirect::to(Request::root().'/auth')-&gt;with("error_message", $msg3);
  
              }
  
          });
  </code></pre>
  
  <p>I am using a Auth::guest() function for stopping unauthorized access but when I hit URL unauthorized access works fine but login fails... without this portion of code login using Auth::attempt() works fine... what can be the problem??</p>
  
  <p><strong>Edit</strong> 
  //AuthController for login</p>
  
  <pre><code>&lt;?php
  
  // uncomment this to use namespaced controller
  //namespace Modules\admin\Controllers;
  
  class AuthController extends \BaseController
  {
      public function __construct()
      {
          $this-&gt;beforeFilter(function()
          {
              Config::set('auth.model', 'User');
          });
      }
  
  
  
      public function getIndex()
      {
  
          return \View::make("login");
      }
  
  
      public function postLogin()
      {
          $msg7  = "Invalid email address or password";
          // $results['userData'] = user::get();
          // dd($results);
          //$password=Input::get('password');
          //$password=Hash::make('secret');
  
          $userData = array(
          'email' =&gt; Input::get('email'),
          'password'=&gt; Input::get('password')
          );
  
           $email=Input::get('email');
          // $password=Input::get('password');
    //    dd($password);
          //dd($userData);
          $rules = array(
          'email' =&gt; 'required|email',
          'password' =&gt; 'required|min:5'
          );
  
  
  
          $remember=Input::get('remember');
  
          $remember_me=false;
          if (!empty($remember)) {
              $remember_me=true;
          }
  
          $validator = Validator::make(Input::get(), $rules);
          if($validator-&gt;fails()){
  
              // redirect to the login page with validation errors
              return Redirect::to(Request::root().'/auth')-&gt;withErrors($validator)-&gt;withInput();
  
  
          }
  
  
          else{
  
              //dd(Auth::attempt($userData));
              // check authentication
              if(Auth::attempt($userData,$remember_me)){
                  // dd(Auth::attempt($userData));                // redirect to the dashboard page
                   //dd($userData);
  
                  return Redirect::to(Request::root().'/home');
  
              }else{
                  //dd($userData);
                  //DB::table('user')-&gt;insert($userData);             
                  //dd("test");
                  // redirect to the login page with error message
                  return Redirect::to(Request::root().'/auth')-&gt;with("error_message", $msg7);
              }
  
          }
      }
  
  
  // logout function
      public function getLogout()
      {
          // delete all data from sesstion
          Auth::logout();
          Session::flush();
          // redirect to login page
          return Redirect::to(Request::root().'/auth');
      }
  
  }
  </code></pre>
  
  <p>// DashboardController where unauthorized access is blocked</p>
  
  <pre><code>&lt;?php
  
  class DashboardController extends BaseController{
  
      public function __construct()
      {
          $this-&gt;beforeFilter(function()
          {
               Config::set('auth.model', 'User');
  
  
              if ((Auth::guest())) {
              //dd(Auth::guest());
              $msg3  = "Please Login First";
              // dd("ok");
              return Redirect::to(Request::root().'/auth')-&gt;with("error_message", $msg3);
  
              }
  
          });
  
      }
  
  
      public function getIndex()
      {
              //dd(Auth::check());
  
          return View::make('home');
      }
  
  }
  </code></pre>