7.7 KiB
Authentication System Fix - Complete Guide
Issues Fixed
1. Registration 404 Error
Problem: Submitting the registration form resulted in a 404 error.
Root Cause:
- Route cache was stale after adding new controllers
- Development server needed restart after cache clearing
Solution:
- Cleared all Laravel caches (route, config, cache, view)
- Updated super-admin assignment logic in RegisteredUserController
- Created restart script for easy server management
2. Super Admin Assignment
Problem: First user wasn't getting super-admin privileges automatically.
Root Cause:
- Logic was checking
User::count() === 1which could fail if test users existed - RolePermissionSeeder wasn't being called in DatabaseSeeder
Solution:
- Changed logic to check if any user has super-admin role:
!User::whereHas('roles', function ($query) { $query->where('slug', 'super-admin'); })->exists() - Added RolePermissionSeeder to DatabaseSeeder
- This ensures first user without super-admin role gets it, regardless of total user count
3. Password Reset Controllers Missing
Problem: Password reset functionality was incomplete.
Solution:
- Created
PasswordResetLinkControllerfor forgot password - Created
NewPasswordControllerfor password reset form - Added all necessary routes in web.php
Files Modified
1. app/Http/Controllers/Auth/RegisteredUserController.php
// Improved super-admin assignment logic
if (!User::whereHas('roles', function ($query) {
$query->where('slug', 'super-admin');
})->exists()) {
$user->assignRole('super-admin');
}
2. database/seeders/DatabaseSeeder.php
public function run(): void
{
// Seed roles and permissions first
$this->call(RolePermissionSeeder::class);
// ... rest of seeding
}
3. app/Http/Controllers/Auth/PasswordResetLinkController.php
- Created complete controller for password reset link requests
4. app/Http/Controllers/Auth/NewPasswordController.php
- Created complete controller for password reset form handling
How to Use
Step 1: Restart Your Server (Windows)
Option A - Use the restart script (RECOMMENDED):
Simply double-click the restart-server.bat file in your project folder, or run it from command prompt:
restart-server.bat
Option B - Manual restart:
- Stop your current server (press Ctrl+C in the terminal where it's running)
- Clear caches:
php artisan optimize:clear - Start server:
php artisan serve
Note: You're running on Windows, so the .bat file will work perfectly for you!
Step 2: Test Registration Flow
-
Access registration page:
- Navigate to:
http://127.0.0.1:8000/register
- Navigate to:
-
Fill out the form:
- Full Name: Your name
- Email: valid@email.com
- Password: Strong password (min 8 characters)
- Confirm Password: Same password
- Mobile Number: Your phone number
- Gender: Select M or F
- Birthdate: Select date (must be at least 10 years ago)
- Nationality: Select country
-
Submit the form:
- Click "REGISTER" button
- Should redirect to email verification page
- Check console/logs for welcome email
-
Verify super-admin assignment:
SELECT u.id, u.email, r.name as role FROM users u JOIN user_roles ur ON u.id = ur.user_id JOIN roles r ON ur.role_id = r.id WHERE r.slug = 'super-admin';
Step 3: Test Login Flow
-
Access login page:
- Navigate to:
http://127.0.0.1:8000/login
- Navigate to:
-
Login with registered credentials:
- Email or Mobile: Your registered email
- Password: Your password
-
Should redirect to:
/explorepage (clubs explore page)
Step 4: Test Password Reset Flow
-
Access forgot password:
- Navigate to:
http://127.0.0.1:8000/forgot-password
- Navigate to:
-
Request reset link:
- Enter your email
- Submit form
- Check email for reset link
-
Reset password:
- Click link in email
- Enter new password
- Confirm new password
- Submit
Verification Checklist
- Registration page loads without errors
- Registration form submits successfully (no 404)
- User is redirected to email verification page
- Welcome email is sent (check logs if mail not configured)
- First user has super-admin role in database
- Second user does NOT have super-admin role
- Login page loads without errors
- Login works with email
- Login works with mobile number
- Forgot password page loads
- Password reset email is sent
- Password reset form works
- Super-admin can access
/adminroutes
Database Verification Queries
Check if roles are seeded:
SELECT * FROM roles;
Check if permissions are seeded:
SELECT * FROM permissions;
Check user roles:
SELECT u.id, u.name, u.email, r.name as role, r.slug
FROM users u
LEFT JOIN user_roles ur ON u.id = ur.user_id
LEFT JOIN roles r ON ur.role_id = r.id;
Check first user's super-admin status:
SELECT u.*, r.name as role
FROM users u
JOIN user_roles ur ON u.id = ur.user_id
JOIN roles r ON ur.role_id = r.id
WHERE u.id = 1 AND r.slug = 'super-admin';
Troubleshooting
Still Getting 404 Errors?
-
Verify routes are registered:
php artisan route:list --path=register php artisan route:list --path=login php artisan route:list --path=password -
Check if server is running:
- Look for "Laravel development server started" message
- Verify port 8000 is not in use by another process
-
Clear browser cache:
- Hard refresh: Ctrl+Shift+R (Windows) or Cmd+Shift+R (Mac)
- Or use incognito/private browsing mode
-
Check .env file:
APP_URL=http://127.0.0.1:8000
Super-Admin Not Assigned?
-
Check if roles are seeded:
php artisan db:seed --class=RolePermissionSeeder -
Verify role exists:
SELECT * FROM roles WHERE slug = 'super-admin'; -
Check user_roles table:
SELECT * FROM user_roles WHERE role_id = (SELECT id FROM roles WHERE slug = 'super-admin');
Email Not Sending?
-
Check mail configuration in .env:
MAIL_MAILER=log MAIL_FROM_ADDRESS="noreply@example.com" MAIL_FROM_NAME="${APP_NAME}" -
For development, use log driver:
- Emails will be written to
storage/logs/laravel.log
- Emails will be written to
-
Check WelcomeEmail class exists:
php artisan list | grep mail
Production Deployment Notes
Before Deploying:
-
Seed a super-admin user:
php artisan db:seed --class=RolePermissionSeeder -
Create first admin manually:
$user = User::create([...]); $user->assignRole('super-admin'); -
Or use invitation system:
- Implement invite-only registration for first admin
- Require admin approval for subsequent registrations
Security Considerations:
-
Disable public registration after first admin:
- Add middleware to check if super-admin exists
- Redirect to login if registration should be closed
-
Enable email verification:
- Uncomment verification check in AuthenticatedSessionController
- Ensure email service is properly configured
-
Implement rate limiting:
- Add throttle middleware to registration route
- Prevent brute force attacks
-
Add CAPTCHA:
- Implement reCAPTCHA on registration form
- Prevent automated bot registrations
Next Steps
- ✅ Registration system working
- ✅ Login system working
- ✅ Password reset working
- ✅ Super-admin auto-assignment working
- ⏳ Test email verification flow
- ⏳ Test admin panel access
- ⏳ Test role-based permissions
- ⏳ Configure production email service