Basic auth using a Member Model
This project implements a custom authentication system for a "Members" collection by extending the Authenticatable Laravel class. Below is an overview of the features, steps, and considerations involved in setting up this system.
Features
- Member Registration: Members can register with a username, email, and password.
- Member Login: Members can log in with their email and password.
- Member Dashboard: Members can access a dashboard with their profile information.
- Transaction Model: Members can Create, Read, and Delete Transaction information.
- Member Logout: Members can log out of their account.
- Member Profile: Admins can view and edit member profile information from the Statamic Control Panel.
- Member Index: Members can see a list of other existing Members.
Table of Contents
Creating the Members Collection
Step 01:
At the beginning, I was not sure how a Statamic Collection and custom Laravel Model would work together. I spent an entire day attempting to implement a Statamic Collection which used a Repository and defined auth providers and guards. Even after lots of reading, I ended up getting blocked with errors. I could get either a custom Model working and storing in a database, or a collection but only working with local .yaml files. To keep things simple, I created a custom model and utilized the Runway composer package to sync the database and model with the Statamic Control Panel.
In-review:
Another issue I faced initially was hashing the passwords, after some troubleshooting I ensured that passwords were properly hashed before being stored.
Authentication Guard and Provider
Step 02:
I modified the authentication guard and provider configuration in config/auth.php
. The provider tells Laravel how to fetch and verify members (by an eloquent model), and the guard determines how they are authenticated. This step was completely new to me, although I had some confidence in the eloquent model approach from my previous Laravel experience.
In-review:
For next steps additional auth features such as password resets, and email verification would be a great addition.
Routes and Controllers
Step 03:
In the Web.php file, I defined the routes for member registration and login. I also created a MemberAuthController to handle these requests. This step was straightforward, for this project I just set a statamic route to access the $page variable, and utilized middleware to check if the user is authenticated.
In-review:
I believe it would be best to add an auth check for individual members when accessing their dashboard. This would prevent unauthorized access to the dashboard from a different member. Also I believe this controller is a bit too large, and could be broken down into smaller controllers for better organization, including a separate transactions controller.
Views
Step 04:
Generally I didn't spend much time at all with front-end styling. I worked from a component library, modified some styles, and DRY'd some code. I utilized .blade views for all of my pages. I also created components for nav-links and form inputs, and abstracted button and link styles in site.css. Componentization is one of my favorite parts of coding that I always try to improve, keeping code DRY and organized is something I always strive to achieve.