user3242861
user3242861

Reputation: 1929

Laravel - Auth::attempt doesn't work, return false

I'm trying to do a login in laravel but i'm using mssql and my user table named us.

In config/auth.php i changed the providers to change the table name:

'providers' => [
    //'users' => [
    //    'driver' => 'eloquent',
    //    'model' => App\User::class,
    //],

    'users' => [
      'driver' => 'database',
      'table' => 'us',
    ]
],

I also create a middleware named authenticated :

public function handle($request, Closure $next)
{
  if(Auth::check())
  {
    return $next($request);
  }
  else {
    return redirect('/login');
  }

}

Now i create a post route /login and now i'm doing all in route without controller:

 Route::post('/login', function()
{
  echo "login...";
  $username = Input::get('username')."</br>";
  $password = Input::get('password')."</br>";
  $remember = Input::get('remember')."</br>";


  $userdata = array(
    'username'  => Input::get('username'),
    'codeApp'  => Input::get('password')
  );
  $rules = array(
        'username' => 'required|exists:us,username',
        'codeApp' => 'required|exists:us,codeApp'
  );

  $validator = Validator::make($userdata, $rules);

  if ($validator->passes())
  {
    $auth = DB::table('us')->where('username', '=', Input::get('username'))
      ->where('codeApp', '=', Input::get('password'))->get()->first();
    // Try to log the user in.
    if ($auth)
    {
        // Redirect to homepage
        //Auth::login($auth);
        dd(Auth::attempt(['username' => $username, 'codeApp' => $password], $remember));
        if(Auth::attempt(['username' => $username, 'codeApp' => $password], $remember))
        {
          echo "ok";
          return Redirect::to('app/groups');
        }

    }
  }
  else
  {
    echo "error";
    return Redirect::to('login')->withErrors($validator);
  }
});

When i put the correct data in form the $auth obtain the correct user but in dd(Auth::attempt(['username' => $username, 'codeApp' => $password], $remember)); return false and i don't know why.

How can i solve that?

Thanks

Upvotes: 0

Views: 832

Answers (1)

Aleksey Pikulik
Aleksey Pikulik

Reputation: 31

The problem is that in the Route::post('/login', function() {} you assigning wrong values for the variables

$username = Input::get('username')."</br>";
$password = Input::get('password')."</br>";
$remember = Input::get('remember')."</br>";

because you made an extra concatenation of ."</br>" so in the result you are trying to auth with corrupted credentials. To solve simply remove that concatenation.

And one more thing - you don't need to make extra query to the DB and then check the result like you do:

$auth = DB::table('us')->where('username', '=', Input::get('username'))
      ->where('codeApp', '=', Input::get('password'))->get()->first();
    // Try to log the user in.
    if ($auth)
    {}

simply because you have already checked that provider user and appCode exists using validation rules

$rules = array(
        'username' => 'required|exists:us,username',
        'codeApp' => 'required|exists:us,codeApp'
  );

So after validation check simply make Auth::attempt(...) and you are done.

And to make your code even better replace all your Input::get('...') calls with corresponding variable.

And one more advice - don't echo to debug app flow - use \Log::debug('Whatever you want to debug') to write into Laravel's log file which you can trace with tail -f ./storage/logs/laravel.log console command

Upvotes: 1

Related Questions