Laravel Authentication

$this->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')->with("error_message", $msg3);

            }

        });

      

I am using Auth :: guest () function to stop unauthorized access, but when I click on the url, the unauthorized access works fine, but login fails ... without this part of the login in code using Auth :: try () is fine works. what could be the problem?

Edit // AuthController for login

<?php

// uncomment this to use namespaced controller
//namespace Modules\admin\Controllers;

class AuthController extends \BaseController
{
    public function __construct()
    {
        $this->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' => Input::get('email'),
        'password'=> Input::get('password')
        );

         $email=Input::get('email');
        // $password=Input::get('password');
  //    dd($password);
        //dd($userData);
        $rules = array(
        'email' => 'required|email',
        'password' => 'required|min:5'
        );



        $remember=Input::get('remember');

        $remember_me=false;
        if (!empty($remember)) {
            $remember_me=true;
        }

        $validator = Validator::make(Input::get(), $rules);
        if($validator->fails()){

            // redirect to the login page with validation errors
            return Redirect::to(Request::root().'/auth')->withErrors($validator)->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')->insert($userData);             
                //dd("test");
                // redirect to the login page with error message
                return Redirect::to(Request::root().'/auth')->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');
    }

}

      

// DashboardController where unauthorized access is blocked

<?php

class DashboardController extends BaseController{

    public function __construct()
    {
        $this->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')->with("error_message", $msg3);

            }

        });

    }


    public function getIndex()
    {
            //dd(Auth::check());

        return View::make('home');
    }

}

      

+3


source to share


1 answer


The problem is that you have to exclude your actions when submitting login form data. If in your controller you have a method postLogin

to handle POST data from a login form, you should use:

  $this->beforeFilter(function() {
          // here goes code of your filter
  }, array('except' => 'postLogin'));

      



Link

+1


source







All Articles