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,
];