Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 - How to check username & password is match with table?

I have created the below login form in Laravel 5 and I want to simply check if the username & password matches with that of the database table and if so, redirect to the dashboard page else stay on the login page. I am also trying to find the solution by myself but I am posting this question to get an idea of how to do these things in Laravel 5.

Any idea??

2015_09_10_050324_admin_details.php (migrations)

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AdminDetails extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('admin_details', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('username')->unique();
            $table->string('email')->unique();
            $table->string('password', 60);
            $table->integer('status');
            $table->rememberToken();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('admin_details');
    }
}

Database structure

enter image description here

enter image description here

login.blade.php (view)

<form name="frmLogin" action="{{ URL::to('administrator/userAuthentication') }}" method="post">
    <input name="_token" type="hidden" value="{{ csrf_token() }}"/>
    <div class="form-group has-feedback">
        <input type="text" name="username" id="username"class="form-control" placeholder="Username">
        <span class="glyphicon glyphicon-envelope form-control-feedback"></span>
    </div>
    <div class="form-group has-feedback">
        <input type="password" name="password" id="password" class="form-control" placeholder="Password">
        <span class="glyphicon glyphicon-lock form-control-feedback"></span>
    </div>
    <div class="row">
        <div class="col-xs-4">
            <button type="submit" class="btn btn-primary btn-block btn-flat">Login</button>
        </div><!-- /.col -->
    </div>
</form>

AdminLoginController.php (controller)

    <?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use Auth;
use App\Http\Controllers\Controller;
use App\AdminLoginModel;


class AdminLoginController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        return view('backend.login');
    }

    /**
     * Handle an authentication attempt for admin user.
     *
     */
    public function userAuthentication(Request $request)
    {

        if (Auth::attempt(array('username' => $request->username, 'password' => $request->password))){
            return "success";
        }else{
            return "Wrong Credentials";
        }
        die;
    }
}

AdminLoginModel.php (model)

    <?php
/*namespace App;
use DB;
use Illuminate\Database\Eloquent\Model;*/

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 AdminLoginModel extends Model implements AuthenticatableContract, CanResetPasswordContract
{
    use Authenticatable, CanResetPassword;

    protected $table = 'admin_details';
    protected $fillable = ['username', 'password'];
}

routes.php

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::resource('dashboard','DashboardController');
Route::resource('administrator','AdminLoginController');
Route::resource('users','AdminLoginController');
Route::resource('administrator/userAuthentication', 'AdminLoginController@userAuthentication');
like image 928
Mr.Happy Avatar asked Sep 10 '15 08:09

Mr.Happy


1 Answers

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);
    }
like image 80
Vision Coderz Avatar answered Oct 16 '22 20:10

Vision Coderz