How To Switch to Ajax-based Authentication in Laravel 6
In this tutorial, you will learn how to Switch to Ajax-based Authentication in Laravel 6.
One of the exciting features of Laravel 6 the ability to immediately generate a fully usable authentication system by installing the laravel UI package and running the auth command. I.e
composer require laravel/ui
laravel ui vue --auth
As exciting as this feature is, the default authentication system in Laravel (6) is view-based, and a lot of developers who know what this means have the opinion that this is not so User Experience focused.
One way to fix this is by tweaking the default Laravel authentication system from a view-based method to an AJAX powered method.
In this article, you would learn:
- How to Set Up a Laravel Authentication System
- How Laravel’s Authentication System works
- How to overwrite the system to an AJAX powered system.
Without wasting time, let’s get to it.
Setting up a Laravel 6 Application
Laravel 6 authentication scaffolding as we know, moved away from the default way of running:
php artisan make:auth
After Installation, in order to use the authentication scaffolding, we need to require the laravel/ UI package.
To do that, run:
composer require laravel/ui
After the UI package has been installed, we can now run:
php artisan ui vue --auth
On running, this Laravel sets up a basic authentication system for us.
Let’s go on to create an example user with Laravel tinker. To do this, run:
php artisan tinker
this opens up the tinker shell, type, and execute the code below to create a dummy user:
factory(App\User::class)->create();
Switch to an AJAX Based Authentication in Laravel
If we look at the authenticateUsers.php
file we notice that there’s an empty function Laravel provides for us in order to enable developers to customize directives when the user is authenticated.
protected function authenticated(Request $request, $user)
{
//
}
To enable AJAX on login, we are going to remove this block of code and modify our loginController.php
class
to:
namespace App\Http\Controllers\Auth;
use \Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
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');
}
/**
* The user has been authenticated.
*
* @param \Illuminate\Http\Request $request
* @param mixed $user
* @return mixed
*/
protected function authenticated(Request $request, $user)
{
//
return(' Login Successful!');
//The rest of the functions you want to be called can be done here (eg AJAX)
}
}
After saving the file, head over to 127.0.0.1:8000/login
to see your Login page.
Login in with the credentials generated from tinker and you should get a response similar to this:
If you have a response similar to mine above, you have successfully tweaked the default authentication system in Laravel to allow custom AJAX requests.