|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Livewire\Users; |
| 4 | + |
| 5 | +use App\Concerns\PasswordValidationRules; |
| 6 | +use App\Concerns\ProfileValidationRules; |
| 7 | +use App\Models\Social; |
| 8 | +use App\Models\User; |
| 9 | +use Illuminate\Contracts\Auth\MustVerifyEmail; |
| 10 | +use Illuminate\Support\Facades\Auth; |
| 11 | +use Illuminate\Support\Facades\Session; |
| 12 | +use Laravel\Fortify\Actions\DisableTwoFactorAuthentication; |
| 13 | +use Laravel\Fortify\Features; |
| 14 | +use Livewire\Attributes\Computed; |
| 15 | +use Livewire\Attributes\Locked; |
| 16 | +use Livewire\Component; |
| 17 | + |
| 18 | +class Profile extends Component |
| 19 | +{ |
| 20 | + use ProfileValidationRules, PasswordValidationRules; |
| 21 | + |
| 22 | + public $user; |
| 23 | + |
| 24 | + public string $name = ''; |
| 25 | + |
| 26 | + public string $username = ''; |
| 27 | + |
| 28 | + public string $email = ''; |
| 29 | + |
| 30 | + public string $password = ''; |
| 31 | + |
| 32 | + public mixed $socials_google; |
| 33 | + |
| 34 | + public mixed $socials_github; |
| 35 | + |
| 36 | + #[Locked] |
| 37 | + public bool $canManageTwoFactor; |
| 38 | + |
| 39 | + #[Locked] |
| 40 | + public bool $twoFactorEnabled; |
| 41 | + |
| 42 | + public function boot() |
| 43 | + { |
| 44 | + $this->withValidator(function ($validator) { |
| 45 | + $validator->after(function ($validator) { |
| 46 | + if ($validator->errors()->count() > 0) { |
| 47 | + $this->dispatch('toastify', [ |
| 48 | + 'type' => 'error', |
| 49 | + 'message' => $validator->errors()->all()[0] |
| 50 | + ]); |
| 51 | + } |
| 52 | + }); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + public function mount(string $id) |
| 57 | + { |
| 58 | + $this->user = User::getUserWithSocialAccount($id)->first(); |
| 59 | + |
| 60 | + if ($this->user) { |
| 61 | + if ($this->user->id == Auth::user()->id) { |
| 62 | + return $this->redirectRoute('profile', navigate: true); |
| 63 | + } |
| 64 | + |
| 65 | + $this->name = $this->user->name; |
| 66 | + |
| 67 | + $this->username = $this->user->username; |
| 68 | + |
| 69 | + $this->email = $this->user->email; |
| 70 | + |
| 71 | + $this->socials_google = $this->user->socialAccounts()->where('provider', 'google')->first(); |
| 72 | + |
| 73 | + $this->socials_github = $this->user->socialAccounts()->where('provider', 'github')->first(); |
| 74 | + } |
| 75 | + |
| 76 | + $this->canManageTwoFactor = Features::canManageTwoFactorAuthentication(); |
| 77 | + |
| 78 | + if ($this->canManageTwoFactor) { |
| 79 | + $this->twoFactorEnabled = $this->user->hasEnabledTwoFactorAuthentication(); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public function render() |
| 84 | + { |
| 85 | + return view('livewire.users.profile')->layout('layouts::app', [ |
| 86 | + 'title' => $this->name . ' Profile', |
| 87 | + 'user' => Auth::user() |
| 88 | + ]); |
| 89 | + } |
| 90 | + |
| 91 | + #[Computed] |
| 92 | + public function hasUnverifiedEmail(): bool |
| 93 | + { |
| 94 | + return $this->user instanceof MustVerifyEmail && !$this->user->hasVerifiedEmail(); |
| 95 | + } |
| 96 | + |
| 97 | + public function updateProfileInformation(): void |
| 98 | + { |
| 99 | + $validated = $this->validate($this->profileRules($this->user->id)); |
| 100 | + |
| 101 | + $this->user->fill($validated); |
| 102 | + |
| 103 | + if ($this->user->isDirty('email')) { |
| 104 | + $this->user->email_verified_at = null; |
| 105 | + } |
| 106 | + |
| 107 | + $this->user->save(); |
| 108 | + |
| 109 | + $this->dispatch('toastify', [ |
| 110 | + 'type' => 'success', |
| 111 | + 'message' => 'User Profile Updated Successfully!' |
| 112 | + ]); |
| 113 | + } |
| 114 | + |
| 115 | + public function unlinkSocialAccount(string $provider): void |
| 116 | + { |
| 117 | + $socialAccontsById = Social::getUserBySocialAccoutsEmail($provider, $this->user->email); |
| 118 | + |
| 119 | + if ($socialAccontsById->first()) { |
| 120 | + $socialAccontsById->delete(); |
| 121 | + |
| 122 | + if ($provider == 'google') { |
| 123 | + $this->socials_google = null; |
| 124 | + } |
| 125 | + |
| 126 | + if ($provider == 'github') { |
| 127 | + $this->socials_github = null; |
| 128 | + } |
| 129 | + |
| 130 | + $this->dispatch('toastify', [ |
| 131 | + 'type' => 'success', |
| 132 | + 'message' => 'Social Account Unlinked Successfully!' |
| 133 | + ]); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + public function disableTwoFactor(DisableTwoFactorAuthentication $disableTwoFactorAuthentication) |
| 138 | + { |
| 139 | + $disableTwoFactorAuthentication($this->user); |
| 140 | + |
| 141 | + $this->twoFactorEnabled = false; |
| 142 | + |
| 143 | + $this->dispatch('toastify', [ |
| 144 | + 'type' => 'success', |
| 145 | + 'message' => 'Two-Factor Disable Successfully!' |
| 146 | + ]); |
| 147 | + } |
| 148 | + |
| 149 | + public function deleteAccount(): void |
| 150 | + { |
| 151 | + $this->user->oauthApps()->delete(); |
| 152 | + |
| 153 | + $this->user->socialAccounts()->delete(); |
| 154 | + |
| 155 | + $this->user->delete(); |
| 156 | + |
| 157 | + Session::flash('status', 'User Deleted Successfully'); |
| 158 | + |
| 159 | + $this->redirectRoute('users.home', navigate: true); |
| 160 | + } |
| 161 | +} |
0 commit comments