In this episode, we take a quick detour from our original plan to write some tests for our comment functionality. Instead, we identify an annoying issue caused by our temporary approach to "faking" authentication in our routes. This hack was breaking the tests because the database migrations hadn't run yet, so users didn't exist at that point.
To solve this, we decide to scaffold a super basic authentication system—just a login and logout setup (no registration for now). The episode shows the entire process: setting up new controllers for login and logout, writing basic logic for authentication, and handling redirections. Then, we set up the routes and create a very simple login form in the view, keeping the styling consistent with the rest of the app.
We also adjust the navigation to show the correct login/logout options depending on whether the user is logged in or not, and make sure CSRF protection is in place. Some UX touches are added, like keeping the entered email in the form when login fails, and neatly showing error messages for invalid credentials.
By the end of the episode, we've got a functional minimal authentication system that works with our manual testing and, crucially, lets our automated tests run without error. Next up, we'll finally dive into testing our comment posting logic with this improved setup!