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:

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.

Hi, My name is Sanish Gurung. With a strong foundation in PHP and a deep understanding of the Laravel framework, I specialize in developing scalable and efficient web solutions. My journey into the world of programming began with a fascination for crafting elegant code and solving complex problems. Since then, I've dedicated myself to mastering PHP/Laravel and exploring the latest technologies in web development.

Leave a Comment