Today, we are share with you how to modify laravel bydefault authentication and make it custom as per our requirement. in this totorials we are modify laravel authentication sysstem and make "how to login with only mobile number in laravel application using laravel custom authentication". but you also can modify as per your requirement.
Laravel provide bydefault authentication system on users table's email and password fields and it very helpfull for us. but sometime we want to change laravel authentication and make is as per our requirment. Ex. we want username or password, we want mobile number and password and sometime we want only mobile number for authentication and not extra needed. so, how to built laravel custom authentication? we are share here with very simple example. if you fallow all step then you can change laravel auth and set your custom auth as per your choice. it is very easy.
Create Laravel New Project
First, we are create one new fresh laravel project run by following command. if you already have laravel project and you want implement custom auth your exist laravel application. so, this step skeep and move on next.
composer create-project --prefer-dist laravel/laravel blog
After installation please setup your database in .env file
Create Laravel Auth
Next, Generate laravel bydefault auth run by following command
php artisan make:auth
Run Laravel Migration
Befor run laravel migration please add one extra mobile_no field in user table migration open it and add like that.
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('mobile_no')->unique();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Next, then after run laravel migration using following command
php artisan migrate
Change in RedirectIfAuthenticated
Next, now we are need some changes in RedirectIfAuthenticated middleware. so, open your app/Http/Middleware/RedirectIfAuthenticated.php file and make change like that.
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\Auth;
class RedirectIfAuthenticated
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string|null $guard
* @return mixed
*/
public function handle($request, Closure $next, $guard = null)
{
if (Auth::check()) {
return redirect('/home');
}
return $next($request);
}
}
[ADDCODE]
Overwrite login() method
Next, we are need to overwrite login() method. so, open your app/Http/Controllers/Auth/LoginController.php file and make some change following way.
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\User;
class LoginController extends Controller
{
/*
|--------------------------------------------------------------------------
| Login Controller
|--------------------------------------------------------------------------
|
| This controller handles authenticating users for the application and
| redirecting them to your home screen. The controller uses a trait
| to conveniently provide its functionality to your applications.
|
*/
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/home';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('guest')->except('logout');
$this->user = new User;
}
public function login(Request $request)
{
// Check validation
$this->validate($request, [
'mobile_no' => 'required|regex:/[0-9]{10}/|digits:10',
]);
// Get user record
$user = User::where('mobile_no', $request->get('mobile_no'))->first();
// Check Condition Mobile No. Found or Not
if($request->get('mobile_no') != $user->mobile_no) {
\Session::put('errors', 'Your mobile number not match in our system..!!');
return back();
}
// Set Auth Details
\Auth::login($user);
// Redirect home page
return redirect()->route('home');
}
}
Change In Login Blade
Next, open your resources/views/auth/login.blade.php file and make changes this way.
@extends('layouts.app')
@section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Login</div>
<div class="panel-body">
<form class="form-horizontal" method="POST" action="{{ route('login') }}">
{{ csrf_field() }}
<div class="form-group{{ $errors->has('mobile_no') ? ' has-error' : '' }}">
<label for="mobile_no" class="col-md-4 control-label">Enter Mobile No.</label>
<div class="col-md-6">
<input id="mobile_no" type="text" class="form-control" name="mobile_no" value="{{ old('mobile_no') }}" required autofocus>
@if ($errors->has('mobile_no'))
<span class="help-block">
<strong>{{ $errors->first('mobile_no') }}</strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<div class="checkbox">
<label>
<input type="checkbox" name="remember" {{ old('remember') ? 'checked' : '' }}> Remember Me
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-8 col-md-offset-4">
<button type="submit" class="btn btn-primary">
Login
</button>
<a class="btn btn-link" href="{{ route('password.request') }}">
Forgot Your Password?
</a>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Now we are ready to run our example so run bellow command ro quick run:
php artisan serve
Now you can open bellow URL on your browser:
http://localhost:8000/login
If you want to any problem then please write comment and also suggest for new topic for make tutorials in future. Thanks...