authentikate/app/Livewire/Forms/UserProfile.php
Javier Feliz 38c0c70ded
Some checks failed
linter / quality (push) Successful in 3m2s
tests / ci (push) Failing after 7m37s
Bunch of progress
2025-07-27 22:18:35 -04:00

94 lines
2.4 KiB
PHP

<?php
namespace App\Livewire\Forms;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;
use Livewire\Attributes\Validate;
use Livewire\Component;
use Livewire\WithFileUploads;
class UserProfile extends Component
{
use WithFileUploads;
// Profile info
public string $name = '';
public string $email = '';
public ?string $preferred_username = null;
public string $avatar = '';
#[Validate('image|max:10000')]
public $avatarUpload;
// Password
public string $current_password = '';
public string $password = '';
public string $password_confirmation = '';
public function mount(): void
{
$this->name = Auth::user()->name;
$this->email = Auth::user()->email;
$this->preferred_username = Auth::user()->preferred_username;
$this->avatar = Auth::user()->avatar;
}
/**
* Update the profile information for the currently authenticated user.
*/
public function updateProfileInformation(): void
{
$user = Auth::user();
$validated = $this->validate([
'name' => 'required|string|max:255',
'email' => [
'required',
'string',
'lowercase',
'email',
'max:255',
Rule::unique(User::class)->ignore($user->id),
],
'preferred_username' => 'string|max:255'
]);
$user->fill($validated);
if (!empty($this->avatarUpload)) {
if (!empty($user->avatar)) {
Storage::disk('avatars')->delete($user->avatar);
}
$user->avatar = $this->avatarUpload->store(options: 'avatars');
}
$user->save();
$this->dispatch('profile-updated', name: $user->name);
}
/**
* Send an email verification notification to the current user.
*/
public function resendVerificationNotification(): void
{
$user = Auth::user();
if ($user->hasVerifiedEmail()) {
$this->redirectIntended(default: route('dashboard', absolute: false));
return;
}
$user->sendEmailVerificationNotification();
Session::flash('status', 'verification-link-sent');
}
public function render()
{
return view('livewire.forms.user-profile');
}
}