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)
JavaScript
x
<?php
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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
login.blade.php (view)
JavaScript
<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)
JavaScript
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppHttpRequests;
use Auth;
use AppHttpControllersController;
use AppAdminLoginModel;
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)
JavaScript
<?php
/*namespace App;
use DB;
use IlluminateDatabaseEloquentModel;*/
namespace App;
use IlluminateAuthAuthenticatable;
use IlluminateDatabaseEloquentModel;
use IlluminateAuthPasswordsCanResetPassword;
use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;
class AdminLoginModel extends Model implements AuthenticatableContract, CanResetPasswordContract
{
use Authenticatable, CanResetPassword;
protected $table = 'admin_details';
protected $fillable = ['username', 'password'];
}
routes.php
JavaScript
<?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');
Advertisement
Answer
try like this
login.blade.php
JavaScript
<!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:-
JavaScript
<?php namespace App;
use IlluminateAuthAuthenticatable;
use IlluminateDatabaseEloquentModel;
use IlluminateAuthPasswordsCanResetPassword;
use IlluminateContractsAuthAuthenticatable as AuthenticatableContract;
use IlluminateContractsAuthCanResetPassword as CanResetPasswordContract;
class UserRegisters extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
protected $table = 'userregisters';
protected $fillable = ['user_name', 'password'];
}
?>
Controller:-
JavaScript
<?php namespace AppHttpControllers;
use Input;
use AppHttpRequests;
use AppUser;
use AppUserRegisters;
use AppUserProfiles;
use Validator;
use View;
use Auth;
use AppHttpControllersRedirect;
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:-
JavaScript
Route::post('/login', 'UserRegisterController@login');
migration:-
JavaScript
<?php
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
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.