Securing Endpoints with Simple Middleware

Welcome to the next step in securing your Laravel applications! In this unit, we will explore an essential aspect of web development — securing endpoints using middleware. Previously, we learned how to authenticate users with Bcrypt to protect our application. Here, we will build on that knowledge to ensure that only authenticated users can access specific parts of our application. This lesson will help you incorporate a vital layer of security into your web applications.

What You'll Learn

In this section, we will dive into the exciting world of middleware and how it can be used to secure endpoints in Laravel applications. In the previous lesson we covered how to create registration and login functionalities for users. But authentication is only the first step in securing your application. In this lesson, you will learn how to use middleware to restrict access to specific routes based on user authentication. Specifically, you will learn how to secure the todos route so that only authenticated users can access it. Let's get started!

What are Sessions?

Before we dive into the code let's understand the concept of sessions.

A session is a way to store information about a user across multiple pages. When a user logs in, a session is created that stores the user's information. This information is then used to authenticate the user on subsequent requests. Sessions are stored on the server and are accessible across multiple pages.

Let's discuss the analaog of what sessions are in real life. Imagine you are at a party and you are given a wristband to wear. This wristband allows you to access different parts of the party. The wristband is like a session. It stores information about you and allows you to access different parts of the party. In the same way, a session stores information about a user and allows them to access different parts of a website.

As mentioned, the sessions are stored in the backend and they usually have a TTL (Time To Live) which is the time the session will be stored on the server. After the TTL expires, the session is destroyed.

Let's see the steps how this occurs:

  • When the user logs ins, a new session is created for the user ID and the user is authenticated. This is usually achieved by creating a unique token for the user and storing it in the session.
  • This token is then returned to the user and is used to authenticate the user on subsequent requests - this is usually done by storing the token in a cookie or in the local storage of the browser on the client side.
  • When the user makes a request to the server, the server checks the token in the session to authenticate the user. If the token is valid, the user is authenticated and the request is processed. If the token is invalid, the user is redirected to the login page.
  • When the user logs out, the session is destroyed and the user is logged out.
Authentication Middleware
Logging Out
Why It Matters

Securing endpoints is vital in web application development. Middleware acts as a security gate, ensuring that unauthorized users cannot access sensitive parts of your application. By implementing token-based authentication through middleware, you ensure that user data is protected, thereby enhancing your application's reliability and trustworthiness. Middleware not only helps maintain the security of your application but also enforces clean, organized code practices. Ready to strengthen your application's defenses? Let's start practicing!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal