Laravel how to make authentication

Learn how to create authentication using Laravel with a step-by-step guide & example code.

Creating Authentication in Laravel

Authentication is a fundamental part of most web applications. It is the process of verifying a user’s identity before allowing them to access a protected resource. Laravel makes authentication straightforward and easy to implement. It provides a number of features and components that make authentication a breeze.

The most basic way to implement authentication in Laravel is to use the built-in Auth class. This class provides methods for registering, logging in, and logging out users. It also provides methods for checking if a user is authenticated and retrieving the authenticated user's data.

To use the Auth class, we first need to create a User model. This model will represent a user in the application. We can create the model using the make:model command:


php artisan make:model User

Once the model is created, we need to add the authentication fields to the model. These fields will be used to store the user's credentials. We can do this by adding the following to the model:


protected $fillable = [
    'name', 'email', 'password',
];

The next step is to create the routes for the authentication process. We can do this by adding the following routes to the routes/web.php file:


Route::get('login', 'AuthController@login');
Route::post('login', 'AuthController@postLogin');

Route::get('logout', 'AuthController@logout');

Route::get('register', 'AuthController@register');
Route::post('register', 'AuthController@postRegister');

We can then create the controller and add the methods for the authentication process. We can do this by creating a new controller using the make:controller command:


php artisan make:controller AuthController

Once the controller is created, we can add the methods for the authentication process. We can do this by adding the following methods to the controller:


public function login()
{
    // Display the login form
}

public function postLogin(Request $request)
{
    // Validate the user's credentials
    // If valid, log the user in
}

public function logout()
{
    // Log the user out
}

public function register()
{
    // Display the register form
}

public function postRegister(Request $request)
{
    // Validate the user's input
    // If valid, create the user
}

Finally, we need to configure the authentication system. We can do this by adding the following to the config/auth.php file:


'defaults' => [
    'guard' => 'web',
    'passwords' => 'users',
],

'guards' => [
    'web' => [
        'driver' => 'session',
        'provider' => 'users',
    ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => AppUser::class,
    ],
],

Once the authentication system is configured, we can use the Auth class to implement the authentication process. We can do this by calling the methods of the Auth class in the controller methods. For example, we can call the attempt method in the postLogin method to attempt to log the user in:


public function postLogin(Request $request)
{
    // Validate the user's credentials
    if (Auth::attempt($request->only('email', 'password'))) {
        // If valid, log the user in
        return redirect()->intended('dashboard');
    }
}

We can also use the register method to create a new user:


public function postRegister(Request $request)
{
    // Validate the user's input
    $data = $request->only('name', 'email', 'password');
    $user = User::create($data);

    // If valid, create the user
    Auth::login($user);
    return redirect()->intended('dashboard');
}

By following these steps, we can easily implement authentication in Laravel. The Auth class provides many methods for quickly and easily implementing authentication in our applications.

Answers (0)