mirror of
https://github.com/HolgerHatGarKeineNode/einundzwanzig-nostr.git
synced 2026-03-23 19:08:41 +00:00
- 📦 Upgrade Laravel framework, Livewire, and dependencies to ensure compatibility with version `13.1.1`.
71 lines
1.7 KiB
PHP
71 lines
1.7 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Traits;
|
|
|
|
use App\Models\EinundzwanzigPleb;
|
|
use App\Support\NostrAuth;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Livewire\Attributes\On;
|
|
|
|
trait WithNostrAuth
|
|
{
|
|
public ?string $currentPubkey = null;
|
|
|
|
public ?object $currentPleb = null;
|
|
|
|
public bool $isAllowed = false;
|
|
|
|
public bool $canEdit = false;
|
|
|
|
#[On('nostrLoggedIn')]
|
|
public function handleNostrLogin(string $pubkey): void
|
|
{
|
|
$executed = RateLimiter::attempt(
|
|
'nostr-login:'.request()->ip(),
|
|
10,
|
|
function () {},
|
|
);
|
|
|
|
if (! $executed) {
|
|
abort(429, 'Too many login attempts.');
|
|
}
|
|
|
|
NostrAuth::login($pubkey);
|
|
|
|
$this->currentPubkey = $pubkey;
|
|
$this->currentPleb = EinundzwanzigPleb::query()
|
|
->where('pubkey', $pubkey)
|
|
->first();
|
|
|
|
if ($this->currentPleb && in_array($this->currentPleb->npub, config('einundzwanzig.config.current_board'), true)) {
|
|
$this->canEdit = true;
|
|
}
|
|
|
|
$this->isAllowed = true;
|
|
}
|
|
|
|
#[On('nostrLoggedOut')]
|
|
public function handleNostrLogout(): void
|
|
{
|
|
NostrAuth::logout();
|
|
|
|
$this->isAllowed = false;
|
|
$this->currentPubkey = null;
|
|
$this->currentPleb = null;
|
|
$this->canEdit = false;
|
|
}
|
|
|
|
public function mountWithNostrAuth(): void
|
|
{
|
|
if ($user = NostrAuth::user()) {
|
|
$this->currentPubkey = $user->getPubkey();
|
|
$this->currentPleb = $user->getPleb();
|
|
$this->isAllowed = true;
|
|
|
|
if ($this->currentPleb && in_array($this->currentPleb->npub, config('einundzwanzig.config.current_board'), true)) {
|
|
$this->canEdit = true;
|
|
}
|
|
}
|
|
}
|
|
}
|