Create your first laravel app with authentication

Create your first laravel app with authentication

Authentication is one of the most important module or component or plugin for any kind of application either it is web or desktop. We always got first keyword i.e. Authentication in our mind when we thought about development of any new application. In framework, we require a login process for gated content and managing aplication's configurations.

In laravel, we have a amazing component i.e. Authentication which is quite easy to use without any code conflictions with a single command. This component is having all required things for authenticating controllers, routing etc.

Install Authentication

We can easily install this plugin in Laravel with a single artisan command. This command will install complete Authentication component with all controllers and views of a user management system in laravel.

php artisan make:auth

This plugin will create a middleware with name Authenticate for verifying all request to to your laravel application.

Auth Configurations

Now, we need to modify default configuration of this Authentication component. For this activity, we have config file at path [Laravel_Application_Root]/config/ with name i.e. auth.php. In this file, you can easily update authentication process like guards, expiry time etc.

Auth Routings

Routing always play a most important role in setting up of direction of you website request. In case of auth routing, we just need to add only middleware name i.e. auth in routing group for making public pages of your application to gated section. Here is a example of a route setup :

Route::group(['middleware' => 'auth'], function () {
   Route::get('/dashboard', 'DashboardController@index')->name('dashboard');
});

In this example we have added dashboard path under authentication system. Anonymouse users cannot access this dashboard page without login to the application.

Model View Controller

MVC ( Model View Controller ) is lifeline of any framework of any language. This is a pattern or best practice of development for any framework. By installing Authentication pligin, we got some interesting pre developed controllers, models and layouts or views. Auth controllers i.e. RegisterController, LoginController , ForgotPasswordController etc. will be located at path [Laravel_Application_Root]/app/Http/Controllers/Auth. Similarly, views of these controllers will be located at path [Laravel_Application_Root]/resources/views/auth.

Authenticated Users

If we need to do customizations in our custom controllers with existing logged in user, we can easily implement it with the help of Auth Facade

use Illuminate\Support\Facades\Auth;
if (Auth::check()) {
    // Logic [ if user is logged in ]
	$user = Auth::user();
	// Logout User
	auth::logout();
}

Here we are ready with our first laravel application with authentication which totally secured by using CSRF ( Cross-Site Request Forgery ) tokens.