atefrihane/livewire-password-meter
Installation
Run composer require atefrihane/livewire-password-meter and include Alpine.js via CDN:
<script src="//unpkg.com/alpinejs" defer></script>
Basic Usage Add the component to your Livewire form:
<livewire:password-field
wireKey="password"
placeholder="Enter your password"
showStrength="true"
/>
Ensure the wireKey matches the property name in your Livewire component (e.g., public $password).
First Use Case Integrate into a registration/login form to provide real-time password strength feedback, improving UX and security.
Livewire Component Integration
wireKey to bind the input to your Livewire component’s property (e.g., public $password).// In your Livewire component
public $password;
Styling and Customization
:class and :styles for inline or CSS class-based styling.<livewire:password-field
wireKey="password"
class="w-full p-2 border rounded"
styles="color: #333;"
/>
Toggle Password Visibility
:eyeIcon="true" to toggle password visibility dynamically.Strength Meter Feedback
:showStrength="true" to display a visual meter (e.g., bars or percentage) for password strength.Dynamic Rules Extend the default strength logic by overriding the Alpine.js logic in your blade template:
<script>
document.addEventListener('livewire:init', () => {
Livewire.hook('password-meter-updated', (component, strength) => {
// Custom logic (e.g., disable submit if strength < 70%)
if (strength < 70) {
document.querySelector('#submit-button').disabled = true;
}
});
});
</script>
Multi-Field Validation
Combine with other Livewire rules (e.g., rules="min:8|confirmed" in your Livewire component) for layered validation.
Localization Override the strength labels (e.g., "Weak," "Strong") via Alpine.js:
<script>
Alpine.data('passwordMeter', () => ({
strengthLabels: {
0: 'Very Weak',
1: 'Weak',
2: 'Medium',
3: 'Strong',
4: 'Very Strong'
}
}));
</script>
Server-Side Validation Validate the final password in your Livewire component:
protected $rules = [
'password' => 'required|string|min:8|confirmed',
];
Alpine.js Dependency
WireKey Mismatch
wireKey matches the exact property name in your Livewire component (case-sensitive). Example:
<!-- Works -->
<livewire:password-field wireKey="password" />
<!-- Fails (property is $userPassword) -->
<livewire:password-field wireKey="password" />
Strength Logic Overrides
<script>
Alpine.store('passwordMeter', {
calculateStrength(password) {
// Custom implementation
return password.length > 12 ? 4 : 0;
}
});
</script>
Performance with Large Forms
<script>
Alpine.directive('debounce', (el, {value}, {evaluate}) => {
let timeout;
el.addEventListener('input', () => {
clearTimeout(timeout);
timeout = setTimeout(evaluate, 500);
});
});
</script>
Check Livewire Events Use Livewire’s event system to debug:
// In your Livewire component
public function updatedPassword($value) {
$this->dispatch('password-meter-updated', strength: $this->calculateStrength($value));
}
Inspect Alpine.js State Add a temporary debug element to your template:
<div x-data="{ strength: 0 }" x-text="strength"></div>
Clear Cache If styles or functionality break after updates, run:
php artisan view:clear
php artisan cache:clear
Custom Strength Indicators Replace the default meter with a custom component (e.g., a progress bar or icons):
<div x-show="showStrength" x-text="'⚠️' + Array(5).fill().map((_, i) => i < strength ? '✅' : '❌').join('')"></div>
Integration with Livewire Actions Disable submit buttons based on strength:
<button
type="submit"
:disabled="strength < 3"
wire:click="savePassword"
>
Submit
</button>
Server-Side Strength Calculation Offload strength logic to the server for complex rules:
public function calculateStrength($password) {
// Custom server-side logic
return strlen($password) > 10 ? 4 : 0;
}
How can I help you explore Laravel packages today?