Installation
composer require squarenetmedia/auth
Publish the package’s assets and config:
php artisan vendor:publish --provider="SquareNetMedia\Auth\AuthServiceProvider" --tag="auth-views"
php artisan vendor:publish --provider="SquareNetMedia\Auth\AuthServiceProvider" --tag="auth-config"
Configuration
config/auth.php (published by the package) for customization (e.g., redirect paths, guard settings).User model extends SquareNetMedia\Auth\Models\User (or implements SquareNetMedia\Auth\Contracts\Authenticatable).First Use Case: Basic Registration
routes/web.php:
Route::get('/register', [\SquareNetMedia\Auth\Controllers\AuthController::class, 'showRegistrationForm'])->name('register');
Route::post('/register', [\SquareNetMedia\Auth\Controllers\AuthController::class, 'register']);
resources/views/vendor/auth) or extend them.Authentication Stack
Auth facade but provides pre-built controllers (AuthController) and middleware (auth, guest).SquareNetMedia\Auth\Controllers\AuthController to customize logic (e.g., override validateLogin for custom rules).Registration Customization
register method or use form requests:
use SquareNetMedia\Auth\Requests\RegisterRequest;
createUser() in a service or controller to add custom fields:
protected function createUser(array $data) {
return User::create([
...$data,
'custom_field' => $data['custom_field'] ?? null,
]);
}
Password Reset
PasswordResetController or extend it:
Route::post('/password/reset', [\SquareNetMedia\Auth\Controllers\PasswordResetController::class, 'sendResetLink']);
Blade Components
@auth, @guest, and @authenticate directives in Blade templates.resources/views/vendor/auth to resources/views/auth and modifying.API Authentication
config/auth.php:
'api' => [
'driver' => 'sanctum',
'provider' => 'users',
],
users table migration. If you’ve already migrated, skip or modify it.AuthTestCase trait for testing auth flows:
use SquareNetMedia\Auth\Tests\AuthTestCase;
class ExampleTest extends AuthTestCase {
public function test_registration() { ... }
}
AuthController and adding routes for providers (e.g., Google, GitHub).Middleware Conflicts
auth middleware. Check app/Http/Kernel.php for conflicts.View Overrides
--tag="auth-views") will break Blade rendering. Always publish assets first.Sanctum API Tokens
api guard is properly configured in config/auth.php. Missing this will cause 401 errors on API routes.Password Reset Tokens
App\Providers\AppServiceProvider:
use Illuminate\Auth\Notifications\ResetPassword;
ResetPassword::createUrlUsing(...);
User Model Binding
User model implements SquareNetMedia\Auth\Contracts\Authenticatable. If not, override the resolveUser() method in the AuthServiceProvider.config/auth.php:
'log_attempts' => env('AUTH_LOG_ATTEMPTS', true),
.env:
SANCTUM_EXPIRE=PT1H # 1 hour
verified column exists in the users table and the VerifyEmail trait is applied to your User model.Custom Guards
Add a new guard in config/auth.php:
'guards' => [
'admin' => [
'driver' => 'session',
'provider' => 'admins',
],
],
Then create a custom provider and guard.
Two-Factor Auth (2FA)
Integrate with Laravel’s two-factor package and extend the AuthController to handle 2FA flows.
Role-Based Access
Use middleware like role:admin by extending the package’s middleware or integrating with a package like spatie/laravel-permission.
Custom User Fields
Extend the User model and update the registration form/request:
// app/Http/Requests/CustomRegisterRequest.php
public function rules() {
return [
'name' => 'required|string',
'email' => 'required|email',
'custom_field' => 'required|string',
];
}
How can I help you explore Laravel packages today?