Lecture Notes Of Day 10: Authentication Basics

Lecture Notes Of  Day 10

 Authentication Basics

Objective:

By the end of this class, students will be able to set up basic authentication in a Laravel application using Laravel Breeze. This includes creating a user registration system, login functionality, and testing the authentication setup.


Learning Outcomes:

  • Understand what authentication is and why it’s necessary.
  • Install and configure Laravel Breeze for authentication.
  • Set up user registration and login systems.
  • Test authentication to ensure it works as expected.

Prerequisites:

Before starting this lesson, students should have:

  • A Laravel application set up.
  • A basic understanding of Laravel routing, views, and controllers.
  • Composer installed on their system.

1. What is Authentication?

Authentication is the process of verifying the identity of a user. It typically involves a user providing a username (or email) and a password. Once authenticated, the user can gain access to resources within the application. In Laravel, authentication refers to managing users’ login, registration, password resets, and session management.

For this lesson, we will use Laravel Breeze to implement authentication features.


2. Installing Laravel Breeze

Laravel Breeze is a simple, minimal authentication package that provides routes, controllers, and views for basic authentication, including login, registration, password resets, and email verification.

Step 1: Install Laravel Breeze

To begin, we need to install Laravel Breeze via Composer. Open your terminal and run the following command inside your Laravel project:

composer require laravel/breeze --dev

This command installs the Breeze package into your Laravel application, which will give us the basic authentication scaffolding.

Step 2: Install Breeze's Frontend Scaffolding

Next, we'll install the frontend scaffolding that Breeze provides. Run the following command:

php artisan breeze:install

This will install the authentication views (login, registration, etc.) and the necessary controllers and routes for authentication.

Step 3: Install NPM Dependencies

After installing Breeze, we also need to install the required JavaScript and CSS dependencies. This is done using npm (Node Package Manager). Run the following commands in your terminal:

npm install

Then, compile the assets using:

npm run dev

This will compile and prepare the frontend assets (like CSS and JavaScript) for the application.


3. Run Migrations

Laravel uses migrations to create and modify database tables. Breeze requires a user table to store the user data. The migration file for this table is already included in the Breeze package.

To run the migrations and create the required database tables, execute the following command:

php artisan migrate

This command will create the users table in your database with the necessary columns (such as name, email, password, etc.).


4. Testing Authentication

Now that Breeze is installed and the migrations have been run, you can start testing the authentication system.

Step 1: Running the Development Server

You can run the Laravel development server to check your application:

php artisan serve

Visit the application in your browser, typically at http://localhost:8000.

Step 2: Access the Authentication Routes

Breeze automatically provides the following routes:

  • Registration Page: /register
  • Login Page: /login

You can navigate to these pages in your browser. You’ll see the login and registration forms that were automatically generated by Breeze.

Step 3: Register a New User

Click on the "Register" link to create a new user. Fill in the required details (like name, email, password) and submit the form. After successful registration, you will be logged in automatically.

Step 4: Login with a Registered User

If you log out or visit the login page (/login), you can log in with the email and password you registered with.

If everything is set up correctly, after logging in, you should be redirected to the home page, or any other route you've configured for authenticated users.


5. Testing Features

Authentication Middleware

Laravel automatically adds authentication middleware to routes that need to be protected. For example, any route defined in web.php as protected by auth middleware will require the user to be logged in.

You can test this by visiting any route that’s protected with auth middleware. For example:

Route::get('/dashboard', function () {

    return view('dashboard');

})->middleware('auth');

If the user is not logged in and tries to visit this route, they will be redirected to the login page.


6. Conclusion

In this lesson, we have learned how to:

  • Install Laravel Breeze for simple authentication.
  • Set up user registration and login functionality.
  • Test authentication to ensure that the system works properly.

This gives you the basic building blocks for implementing user authentication in a Laravel application. In future lessons, we will explore how to extend and customize the authentication system.


Additional Resources:


Assignment:

  • Implement a registration system where users can sign up with their name, email, and password.
  • Set up a login system and ensure that authenticated users are redirected to a protected page, such as a dashboard.


Comments

Popular posts from this blog

MCQs Of Day CRUD 8 Operations with Eloquent

Lecture Notes Of Day 24: Laravel Policies

MCQs of Day 7 Eloquent ORM Basics