0

I am trying to follow Solution given here.

getAuthPassword() returns the correct hash value yet I am not being authenticated. Code snippet given below:

In Controller:

    public function doLogin()
    {
//        View::composer('layouts.master', function($view)
//        {
//
//            $view->with('login_status');
//        });
        $rules = array(
            'username'    => 'required',
            'password' => 'required'
        );


        // run the validation rules on the inputs from the form
        $validator = Validator::make(Input::all(), $rules);


        if ($validator->fails())
        {
            return Redirect::to('login')
                ->withErrors($validator)
                ->withInput(Input::except('password'));
        }
        else
        {



            $credentials['username'] = trim(Input::get('username'));
            $credentials['password'] = trim(Input::get('password'));

            //if (Auth::attempt(array($credentials), true))
            if (Auth::attempt($credentials))
            {
                echo 'Success';
            }
            else
            {
                print 'faled';
                dd(DB::getQueryLog());
                exit;
                return Redirect::to('login');

            }

        }

User Model

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password', 'remember_token');

    public function getAuthPassword()
    {
        return $this->password;
    }

}

Data in Db enter image description here if it is running correct password the why is it getting failed?

Community
  • 1
  • 1
Volatil3
  • 14,253
  • 38
  • 134
  • 263
  • Have you checked the values of `Input::get()` to make sure they are correct? – Laurence Feb 21 '15 at 10:30
  • @TheShiftExchange I guess `getAuthPassword()` returns password after authentication so I guess its right? – Volatil3 Feb 21 '15 at 10:31
  • No - that has nothing to do with what I asked. If you do `dd(Input::all());` before you try the Auth - are the values there correct? – Laurence Feb 21 '15 at 10:32
  • I get user name and plaintext password – Volatil3 Feb 21 '15 at 10:33
  • How are you originally hashing the passwords? – Laurence Feb 21 '15 at 10:34
  • @TheShiftExchange by using HAsh::make() – Volatil3 Feb 21 '15 at 10:34
  • Is there anyway I can use some Loggin mechanism to figure out which query is being executed? Something similar to Rails? – Volatil3 Feb 21 '15 at 10:34
  • Is the password field in your DB long enough? It should be 60 characters or the hash will be truncated – lukasgeiter Feb 21 '15 at 10:38
  • @kamlesh.bar Interesting doing this gave another error now `select * from `users` where `0` = adnan` – Volatil3 Feb 21 '15 at 10:46
  • @Volatil3 Why are you using `getAuthPassword()` anyways when your column has the default name? (`password`) – lukasgeiter Feb 21 '15 at 10:47
  • @Volatil3 sorry about that it will require couple more field like remember_token in DB and that's not problem with you. for Auth::attempt you just need to have two fields username and password in DB users – kamlesh.bar Feb 21 '15 at 10:48
  • @lukasgeiter I was just trying to set it explicitly. It did not help either – Volatil3 Feb 21 '15 at 10:51
  • @Volatil3 can you please put your database structure above. if you have remember_token field i suggest you add second params attempt to false/true – kamlesh.bar Feb 21 '15 at 10:59
  • @TheShiftExchange Is there ayway I can see query behind it? – Volatil3 Feb 21 '15 at 10:59
  • Event::listen('illuminate.query', function($sql) { Log::error($sql); }); // put this in routes.php file to get query – kamlesh.bar Feb 21 '15 at 11:00
  • @Volatil3 you can access ran queries with `DB::getQueryLog()` – lukasgeiter Feb 21 '15 at 11:00
  • @lukasgeiter where do I put that? – Volatil3 Feb 21 '15 at 11:01
  • @Volatil3 Somewhere after `Auth::attempt()` in this case I'd suggest in the else block. Simply add a line with `dd(DB::getQueryLog())` to get it printed in your browser. – lukasgeiter Feb 21 '15 at 11:03
  • @lukasgeiter it returned this: `faledarray(1) { [0]=> array(3) { ["query"]=> string(50) "select * from `users` where `username` = ? limit 1" ["bindings"]=> array(1) { [0]=> string(6) "adnan" } ["time"]=> float(0.96) } }` It is not checking password field yet getting failed – Volatil3 Feb 21 '15 at 11:07
  • 1
    It wont check the password in the sql function - it is just trying to match a username first – Laurence Feb 21 '15 at 11:08
  • Do you have a user in your database `username` with `adnan`? – Laurence Feb 21 '15 at 11:09
  • @TheShiftExchange Yes and this is why it is returning record in Query in my SQL client – Volatil3 Feb 21 '15 at 11:09
  • Your going to have to post your full view, controller, model and a database schema if we are going to work this out. There is something wrong - but its not in the code above – Laurence Feb 21 '15 at 11:12
  • @TheShiftExchange Question Updated – Volatil3 Feb 21 '15 at 11:18
  • Can you also post where you create the user? I.e. where/how you are hashing the password – Laurence Feb 21 '15 at 11:19
  • 1
    @TheShiftExchange Found the issue. I was calling calling `$faker->username` in the loop twice. So by the time `$faker->username` was set in `password` field it was already changed. Thanks for the input. Please make your comment an Answer – Volatil3 Feb 21 '15 at 11:31

0 Answers0