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

Let's now see how we can implement this logic with a real example.

Let's start by the controller, where we will modify the login method to store the user's ID in the session. This will allow us to authenticate the user on subsequent requests.

Let's examine the code above thoroughly:

  • We have added a new method getSessionData that reads the session data from a file at storage/app/session_data.json. This file will store the session data for all users. Each entry is a key-value pair where the key is the user ID and the value is the session data for that user (which includes the user ID and the token).
  • We have added a new method saveSessionData that writes the session data to the file at storage/app/session_data.json.
Logging Out

Finally, let's see how we can implement a logout functionality. When a user logs out

Notice, that in this method we simply remove the user from the session data. This effectively logs the user out. The client-side code for logging out will look like this:

When the user clicks the Logout button, the logout function is called. This function removes the token from the local storage and sends a POST request to the /logout endpoint with the user ID. The user is then logged out and the session data is updated.

With this setup, you have implemented a complete authentication system with login, logout, and session management in your Laravel application. This adds an extra layer of security to your application and ensures that only authenticated users can access sensitive data.

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