Speeding Up Laravel Tests with tmpfs (MySQL in RAM)
If your Laravel test suite is painfully slow and does a lot of database work (multi-tenancy, migrations, seeding), the bottleneck is almost…
If you provide services to free users in your Laravel Jetstream application, you might want to prevent people from creating multiple accounts from the same IP address. Here's a straightforward way to implement this restriction.
The cleanest way to add this restriction is by modifying the CreateNewUser action that Jetstream provides. This class handles the user registration process, making it the perfect place to add our IP-based validation.
First, add a configuration value to config/auth.php:
'max_signups_per_ip' => env('MAX_SIGNUPS_PER_IP', 3),
And in your .env file:
MAX_SIGNUPS_PER_IP=3
In your app/Actions/Fortify/CreateNewUser.php, add the IP checking logic:
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Config;
class CreateNewUser implements CreatesNewUsers
{
protected function checkIpLimit(string $ip): bool
{
try {
$redisKey = 'signups:' . $ip;
$maxSignups = Config::get('auth.max_signups_per_ip', 3);
$trackingPeriod = 86400; // 24 hours
$currentCount = Redis::get($redisKey);
if ($currentCount === null) {
Redis::setex($redisKey, $trackingPeriod, 1);
return true;
}
if (intval($currentCount) >= $maxSignups) {
return false;
}
Redis::incr($redisKey);
return true;
} catch (\Exception $e) {
\Log::error('Redis error in IP signup check', [
'error' => $e->getMessage(),
'ip' => $ip
]);
return true; // Fail open if Redis is down
}
}
public function create(array $input): User
{
Validator::make($input, [
'name' => ['required', 'string', 'max:255'],
'email' => [
'required',
'string',
'email',
'max:255',
'unique:users',
function ($attribute, $value, $fail) {
if (!$this->checkIpLimit(request()->ip())) {
$fail('Maximum number of accounts reached from this IP address.');
}
},
],
'password' => $this->passwordRules(),
'terms' => Jetstream::hasTermsAndPrivacyPolicyFeature() ? ['accepted', 'required'] : '',
])->validate();
// ... rest of the create method
}
}
The implementation uses Redis to track how many accounts have been created from each IP address:
The implementation includes error handling for Redis failures. If Redis is unavailable, the system will:
When implementing IP-based restrictions, keep in mind:
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com