Overview
The Restaurant Management System implements a robust multi-role authentication system using:- Laravel Sanctum for API token authentication
- Spatie Laravel Permission for role-based access control (RBAC)
- Laravel Jetstream for authentication scaffolding
User Model
The User model extends Laravel’s Authenticatable class and integrates multiple traits for comprehensive authentication features.app/Models/User.php
The
HasRoles trait from Spatie provides methods like hasRole(), hasAnyRole(), and assignRole() for role management.Available Roles
The system supports three primary roles with distinct permissions:- Admin
- Chef
- Mesero (Waiter)
Full System Access
- User management (create, edit, delete users)
- Chef management
- View and manage all orders
- Access to all food, table, and reservation operations
- Complete dashboard access
Role-Based Middleware
The system uses a customRoleMiddleware that supports both Spatie roles and fallback usertype checking.
RoleMiddleware Implementation
app/Http/Middleware/RoleMiddleware.php
Route Protection
Routes are protected using middleware with role specifications:routes/web.php
Post-Login Redirection
After successful authentication, users are redirected based on their role:app/Http/Controllers/HomeController.php
The redirect logic first checks for Spatie’s
hasRole() method, then falls back to a usertype column for backward compatibility.Authentication Flow
User Registration & Login
User Registration & Login
- Registration: New users register through Laravel Jetstream’s registration form
- Role Assignment: Admins assign roles to users through the admin panel
- Login: Users authenticate with email and password
- Redirection: System redirects based on assigned role
- Session Management: Laravel Sanctum manages API tokens and sessions
Protected Routes
Protected Routes
All admin panel routes require authentication:Cart and order routes require basic authentication:
API Token Authentication
The system uses Laravel Sanctum for API token management:Provides token generation and validation methods for API authentication
Protects API routes and validates bearer tokens
Security Features
- Password Hashing: Automatic bcrypt hashing via
passwordcast - Email Verification: Built-in email verification support
- CSRF Protection: Laravel’s built-in CSRF middleware
- Remember Token: Secure “remember me” functionality
- Role Validation: Middleware prevents unauthorized access
Best Practices
Important: Always assign roles to new users immediately after creation to ensure proper access control.
- Use Spatie Permission: Leverage
hasRole()andhasAnyRole()for role checks - Middleware Protection: Apply role middleware to all sensitive routes
- Graceful Fallback: Implement usertype fallback for legacy support
- Centralized Redirects: Use the
redirects()method for consistent post-login behavior - Audit Logs: Consider logging role assignments and permission changes