Guide to Laravel Authentication

Authentication in Laravel is really easy and simple to handle. The application needs to have authentication for security reasons. If you want to configure the authentication service then you can edit the config/auth.php file. Here in this document, we will discuss how to create a Laravel authentication system.

First of all, let’s install laravel/ui before starting this tutorial. You can install the package by using the command below:

composer require laravel/ui

php artisan ui vue --auth

laravel/ui aids in developing scaffolds for routes and views quickly. In this example, we have scaffolded the project with Vue but other components like React and Bootstrap can also be done.

After the completion of the above process, install npm dependencies to your project by running the command below:

npm install

After installation, run npm run dev to compile assets:

npm run dev

Authentication Testing

Now let’s check if authentication is successfully installed in our project or not. For this, we will check the login and registration section.

myproject.com/login

myproject.com/register

Restrict Routes

After authentication testing, we will see how to restrict routes for unauthenticated users.

Route::get('dashboard', 'UserController@dashboard')->middleware('auth');

Alternatively, we can also write code in the controller’s constructor method:

<?php

class UserController extends Controller
{
	public function __construct()
	{
	    $this->middleware('auth');
	}

	public function dashboard(){
		//
	}
}

Both methods function in the same way even if they are in different formats. In Laravel, we can also check if a user is authenticated or not and also get the authenticated user’s data.

if(auth()->check()){
  // If the user only authenticated
}

$user = auth()->user();     // get authenticated user's data

Path Customization

By default, the user is directed towards /home URI if he/she is successfully authenticated. User can configure the default path according to their wish in RouteServiceProvider:

public const HOME = '/profile';

If the user needs powerful customization of response then it is authenticated(Request $request, $user). This can be found inside AuthenticatesUsers traits. Users can define authenticated methods inside LoginController:

/**
 * The user has been authenticated.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  mixed  $user
 * @return mixed
 */
protected function authenticated(Request $request, $user)
{
    return response([
        //
    ]);
}

Username Customization

For authentication, Laravel uses the email field by default. If a user wants to customize then he/she can change it to username later on.

public function username()
{
    return 'username';
}

Similarly, users can also customize other various components like storage, guard, and so on. For additional information, users can look into Laravel documentation.

Hope you like this tutorial 🙂 be sure to check our other tutorials as well.

Asmit Nepali, a Full Stack Developer, holds a Software Engineering degree from Gandaki College of Engineering and Science. Proficient in PHP Laravel, Vue.js, MySQL, Tailwind, Figma, and Git, he possesses a robust technical skill set.

Leave a Comment