Skip to content

Instantly share code, notes, and snippets.

@neverything
Created October 19, 2023 12:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neverything/5cd802e805813f4dc2a2c64444087028 to your computer and use it in GitHub Desktop.
Save neverything/5cd802e805813f4dc2a2c64444087028 to your computer and use it in GitHub Desktop.
<?php
use App\Providers\RouteServiceProvider;
use App\Models\User;
use Illuminate\Auth\Events\Lockout;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;
use Livewire\Attributes\Layout;
use Livewire\Attributes\Rule;
use App\Events\RequestMagicLoginLink;
use Livewire\Volt\Component;
new #[Layout('layouts.guest')] class extends Component {
public string $email = '';
public function login(): void
{
$this->validate(
[
'email' => ['required', 'email', 'exists:users,email'],
],
['email.exists' => 'Register before you login.']
);
$this->ensureIsNotRateLimited();
if ($user = User::where('email', $this->email)->first()) {
event(new RequestMagicLoginLink($user));
RateLimiter::clear($this->throttleKey());
session()->regenerate();
session()->flash('status', 'Check your email! Login link sent!');
}
}
protected function ensureIsNotRateLimited(): void
{
if (!RateLimiter::tooManyAttempts($this->throttleKey(), 5)) {
return;
}
event(new Lockout(request()));
$seconds = RateLimiter::availableIn($this->throttleKey());
throw ValidationException::withMessages([
'email' => trans('auth.throttle', [
'seconds' => $seconds,
'minutes' => ceil($seconds / 60),
]),
]);
}
protected function throttleKey(): string
{
return Str::transliterate(Str::lower($this->email).'|'.request()->ip());
}
}; ?>
<div>
<!-- Session Status -->
<x-auth-session-status class="mb-4" :status="session('status')"/>
<form wire:submit="login">
<!-- Email Address -->
<div>
<x-input-label for="email" :value="__('Email')"/>
<x-text-input wire:model="email" id="email" class="block mt-1 w-full" type="email" name="email" required
autofocus autocomplete="username"/>
<x-input-error :messages="$errors->get('email')" class="mt-2"/>
</div>
<div class="flex items-center justify-end mt-4">
<x-primary-button class="ml-3">
{{ __('Get Magic Login Email') }}
</x-primary-button>
</div>
</form>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment