Laravel Fortify - Implementing Authentication Scaffolding in Laravel8
Laravel fortify is a Frontend agnostic authentication backend implementation for Laravel. Fortify registers the routes and controllers needed to implement all of laravel website development authentication features including registration, passwords, login and more. Since Fortify does not accommodate its own user interface, it is paired with your own interface which makes requests to the routes that Fortify has registered.
Why to use Laravel Fortify?
If anyone uses Laravel’s application starter kits, then one does not need to install Laravel Fortify as all of Laravel’s application starter kits already provide a full authentication implementation. But if we are not using an application starter kit and application needs authentication features, then we have two options: First is manually implement application’s authentication features and secondly use Laravel Fortify to provide the backend implementation of these features.
Choosing to install Fortify, the user interface will make requests to Fortify’s authentication routes that are detailed in documentation in order to authenticate and register users. And instead of using Fortify, choosing manual interaction with Laravel’s authentication service by following the documentation available in the authentication, password reset, and email verification documentation.
The Fortify configuration files consist of a features configuration array. This array explains which backend routes/features Fortify will expose by default. If one does not use Fortify in combination with Laravel Jetstream, it is recommended to enable only some features that are basic authentication features provided by most Laravel applications: Registration, Reset passwords, email verification.
Steps for Laravel 8 Authentication
- Create a new Laravel 8 app - First of all, create a Laravel 8 project Composer create-project laravel
- Install the Laravel UI package - Installing the Laravel UI official package for making auth scaffolding in Laravel8. Then run the composer command to install the Laravel UI package.
- Composer require laravel/ui Generate authentication scaffolding - After installing Laravel UI packages, we are now able to scaffold our authentication with Bootstrap, Vue, React.
- Install NPM dependency - To install NPM dependencies run the following command npm install And to compile assets run the following command Npm run dev
- Test the authentication system - When Laravel 8 auth system is ready, check whether authentication is successfully installed or not Login check: example.com/login - Registration check: example.com/register
- Route restriction - After successfully installation of Laravel 8 auth system. We can protect our routes for unauthenticated users by using auth middleware in our routes or controller.
- Change the Necessary Configuration - When user successfully login the default auth system redirects the user to home/path. If we want to change the path we have to change public const HOME = ‘ /home’; from the RouteServiceProvider
Laravel Frontend Scaffolding
Frontend Scaffolding means to create a basic structure for an application. Laravel provides a very simple way to change the front-end presets/scaffolding with any of the other available scaffolding like Bootstrap, Vue and React. Now Generating Scaffold:
- Step1 - To generate scaffolding we need to install laravel/ui, which is composer package and to do that we need to run the command: composer require laravel/ui --dev
- Step2 - After finishing that, we can run the ui artisan command to generate a base scaffolding. For bootstrap: php artisan ui bootstrap For React: php artisan ui react For Vue: php artisan ui Vue This is will create a components directory in resources/js directory
- After running any of the given commands, we will have to install the npm.
- Now we have to run npm command to compile scaffolding: npm run dev
- Generate scaffolding with Authentication - To generate scaffolding with view files for authentication like login and register, then we just have to add ‘auth’ at the end of the commands like: Bootstrap- php artisan ui bootstrap --auth Vue - php artisan ui vue --auth React - php artisan ui react --auth
- Remove Scaffolding - To remove generated scaffolding, we will run following command
Php artisan preset none.
Specifying Additional Conditions If anyone wishes to add extra query conditions to the authentication query in addition to the user’s mail and password. To conduct this, we may simply add the query conditions to the array passed to the attempt method.
As we can see that from above points we have mentioned about Laravel Fortify, its features and other points. We can also use Fortify (without Jetstream) to serve a headless authentication backend for Laravel Application. Within the fortify configuration file, one can also disable entire features of fortify like ability to update profile information and passwords. This article has given various information regarding Laravel Authentication.