Auth::Attempt(username,password) doesn't work

Viewed 136

I am really new to laravel but had experience using PHP, I have this problem with the Auth::attempt() method. It seems that even thought $request->get() gets the value the attempt method doesn't work. I tried checking my Database Table using Schema::hasTable and yes it exists. I am not using the email and password instead I am using a username and password for the login.

LoginController.php

class LoginController extends Controller
{
    public function login(){
        return view('pages.login');
    }

    public function dashboard(){
        return view('pages.dashboard');
    }

public function checklogin(Request $request)
{
    $rules = array(
        'Username'    => 'required|string|min:5',
        'Password' => 'required|min:6'
    );

    $validator = Validator::make($request->all(), $rules);

    if ($validator->fails()) {
        return Redirect::to('/login')
            ->withErrors($validator) // send back all errors to the login form
            ->withInput($request->except('Password')); // send back the input (not the password) so that we can repopulate the form
    }else{
        $userdata = array(
            'Password'  => $request->get('Password'),
            'Username'     => $request->get('Username')
        );
        if (Auth::attempt($userdata,true)) {

            // validation successful!
            echo 'SUCCESS!';
            echo  Auth::user()->FirstName;
        } else {

            // validation not successful, send back to form
            echo "ERROR!";
        }

    }
    //Just checking if the value was really been posted
    echo $request->get('username').$request->get('password');
}

public function logout()
{
 Auth::logout();
 return redirect('/');
}
}

Here is the model I created

UsersInfo.php

use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthAuthenticatable;

class UsersInfo extends Model implements Authenticatable
{
    use AuthAuthenticatable;

    protected $table = 'UsersInfo';

    public $primaryKey = 'id';

    protected $fillable = [
        'Username','Password','MobileNum','AccountNum','FirstName','LastName','Address'
    ];
    public function getAuthPassword(){
        return $this->Password;
      }
}

auth.php

    <?php

return [

    

    'defaults' => [
        'guard' => 'web',
        'passwords' => 'UsersInfo',
    ],

    

    'guards' => [
        'web' => [
            'driver' => 'session',
            'provider' => 'UsersInfo',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'UsersInfo',
            'hash' => false,
        ],
    ],

    

    'providers' => [
        'Users' => [
            'driver' => 'eloquent',
            'model' => App\User::class,
        ],
        'UsersInfo' => [
            'driver' => 'eloquent',
            'model' => App\UsersInfo::class,
        ],

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

    

    'passwords' => [
        'UsersInfo' => [
            'provider' => 'UsersInfo',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

    

    'password_timeout' => 10800,

];
1 Answers

When passing the credentials to attempt the field holding the password must be named password (in this exact case). This is how the user provider knows what field in the credentials is supposed to be the password. The password is something that is checked after a record is found and is not part of the query.

$userdata = [
    'password' => $request->input('Password'),
    'Username' => $request->input('Username'),
];

Passwords are hashed, you can not directly compare this to the database value, the password field is not part of the query to find the user. Every thing else in the credentials array is a where condition.

Related