homeos/app/Livewire/Modals/RingConnect.php

62 lines
1.6 KiB
PHP

<?php
namespace App\Livewire\Modals;
use App\Models\Addon;
use App\Services\AddonService;
use LivewireUI\Modal\ModalComponent;
/**
* Captures the Ring account login. We never call Ring ourselves (no local API; the ring-mqtt
* sidecar owns the OAuth + 2FA exchange, per the handoff) — the entered credentials are stored
* encrypted and handed to that backend, which completes the connection and reports over MQTT.
*/
class RingConnect extends ModalComponent
{
public string $email = '';
public string $password = '';
public string $code = '';
public bool $hasStoredPassword = false;
public static function modalMaxWidth(): string
{
return 'lg';
}
public function mount(): void
{
$addon = Addon::where('key', 'ring')->first();
$config = $addon?->config ?? [];
$this->email = $config['email'] ?? '';
$this->hasStoredPassword = filled($config['password'] ?? null);
}
public function save()
{
$this->validate([
'email' => 'required|email|max:190',
// Password may be left blank to keep a previously stored one.
'password' => $this->hasStoredPassword ? 'nullable|string|max:190' : 'required|string|max:190',
'code' => 'nullable|string|max:12',
]);
app(AddonService::class)->saveConnection('ring', [
'email' => $this->email,
'password' => $this->password,
'code' => $this->code,
]);
$this->closeModal();
return redirect()->route('addons.index');
}
public function render()
{
return view('livewire.modals.ring-connect');
}
}