Laravel 5 - How to check username & password is match with table?
try using auth attempt
$email=$request->email;
$password=$request->password;
if(Auth::attempt(['email'=>$email,'password'=>$password]))
{
return redirect()->intended('admin/dashboard');
}
this will check authentication
Here you can read official documentation
http://laravel.com/docs/5.1/authentication#authenticating-users
Update
first you need to create table called users
id|username|password|email|remember_token|created_at|updated_at
then in your user model
protected $table = 'users';
protected $fillable = ['username', 'email', 'password'];
whichever column you want to insert data that should write in fillable array and created_at and updated_at type is datatime in mysql so it automatically insert data and time
In your user controller
public function loginPost(Request $request)
{
$email=$request->email;
$password=$request->password;
if(Auth::attempt(['email'=>$email,'password'=>$password]))
{
return redirect()->intended('admin/dashboard');
}
return Redirect::to('login');
}
and note that auth::attempt will automatically hash password so you no need to hash password.
Before login authentication insert one record and Hash password.
$data=[];
$data['email']=$request->email;
$data['password']=Hash::make($password);
User::create($data);
update 2
public function insert()
{
$data=[];
$data['email']=$request->email;
$data['password']=Hash::make($password);
AdminLoginModel::create($data);
}
try like this
login.blade.php
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<h3>Login Form</h3>
{!! Form::open(array('url' => 'login', 'method' => 'post')) !!}
<div class="form-group">
{!! Form::label('UserName') !!}
{!! Form::text('username', null,
array(
'class'=>'form-control',
'placeholder'=>'Your UserName')) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::text('password', null,
array(
'class'=>'form-control',
'placeholder'=>'Your Password')) !!}
</div>
<div class="form-group">
{!! Form::submit('Login',
array('class'=>'btn btn-primary')) !!}
</div>
{!! Form::close() !!}
</div>
</body>
</html>
Model:-
<?php namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class UserRegisters extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
protected $table = 'userregisters';
protected $fillable = ['user_name', 'password'];
}
?>
Controller:-
<?php namespace App\Http\Controllers;
use Input;
use App\Http\Requests;
use App\User;
use App\UserRegisters;
use App\UserProfiles;
use Validator;
use View;
use Auth;
use App\Http\Controllers\Redirect;
use Session;
use Hash;
use DB;
class UserRegisterController extends Controller
{
/**
* Login a Registered Users.
*
*/
public function login(){
$uname = Input::get('username');
$password = Input::get('password');
if (Auth::attempt(array('user_name' => $uname, 'password' => $password))){
return "success";
}
else {
return "Wrong Credentials";
}
}
}
}
route:-
Route::post('/login', 'UserRegisterController@login');
migration:-
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class Userregisters extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('userregisters', function($table)
{
$table->increments('id');
$table->string('first_name', 128);
$table->string('last_name', 128);
$table->string('user_name', 128);
$table->string('password', 128);
$table->string('email', 128);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('userregisters');
}
}
let me know if there any errors.