PHP code example of martin6363 / laravel-api-auth
1. Go to this page and download the library: Download martin6363/laravel-api-auth library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
martin6363 / laravel-api-auth example snippets
use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable;
// ...
}
'user_model' => \App\Models\User::class,
'token' => [
'name' => 'auth_token', // Token name
'abilities' => ['*'], // Token abilities
'expires_at' => null, // Token expiration (null = no expiration)
],
'routes' => [
'prefix' => 'api/auth', // Route prefix
'middleware' => ['api'], // Route middleware
'enabled' => [
'register' => true,
'login' => true,
'logout' => true,
'forgot_password' => true,
'reset_password' => true,
'email_verification' => true,
'profile' => true,
'refresh_token' => true,
],
],
'email_verification' => [
', // Auto-send verification email
],
'emails' => [
'dispatch_mode' => 'queue', // 'queue' or 'sync'
'theme' => [
'primary_color' => '#4f46e5',
'button_text_color' => '#ffffff',
],
],
'password' => [
'min_length' => 8, // Minimum password length
'
'rate_limiting' => [
'enabled' => true, // Enable rate limiting
'max_attempts' => 5, // Max attempts per minute (login/register)
'password_reset_max_attempts' => 3, // Max attempts for password reset
],
'login' => [
'fields' => [
'login' => [',
// The database columns to search for the user
'search_columns' => ['email',],
],
'validation' => [
'name' => ['ng', 'email', 'max:255'],
'password' => ['username' => ['
protected $fillable = [
'name',
'email',
'password',
'phone', // Add your custom field
'username', // Add your custom field
];
'validation' => [
'name' => ['ng', 'email', 'max:255'],
'password' => ['
use Martin6363\ApiAuth\Services\v1\AuthService;
$this->app->bind(AuthService::class, function ($app) {
return new CustomAuthService();
});
'routes' => [
'enabled' => [
'register' => false, // Disable registration
'login' => true,
// ...
],
],
'routes' => [
'prefix' => 'api/v1/auth', // Custom prefix
],
bash
composer vendor:publish --provider="Laravel\Sanctum\SanctumServiceProvider"
php artisan migrate
bash
php artisan api-auth:uninstall
bash
php artisan vendor:publish --tag=api-auth-logic
bash
php artisan test