forge/app/Livewire/Profile/UpdatePasswordForm.php
Refringe 46550b5d8f
OAuth Account Password Creation
This allows a user that was created via OAuth to set a local password on their account.
2024-09-27 16:51:13 -04:00

51 lines
1.6 KiB
PHP

<?php
namespace App\Livewire\Profile;
use App\Actions\Fortify\PasswordValidationRules;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Laravel\Fortify\Contracts\UpdatesUserPasswords;
use Laravel\Jetstream\Http\Livewire\UpdatePasswordForm as JetstreamUpdatePasswordForm;
class UpdatePasswordForm extends JetstreamUpdatePasswordForm
{
use PasswordValidationRules;
/**
* Update the user's password.
*
* This method has been overwritten to allow a user that has a null password to set a password for their account
* without needing to provide their current password. This is useful for users that have been created using OAuth.
*/
public function updatePassword(UpdatesUserPasswords $updater): void
{
$this->resetErrorBag();
$user = Auth::user();
if ($user->password !== null) {
parent::updatePassword($updater);
} else {
// User has a null password. Allow them to set a new password without their current password.
Validator::make($this->state, [
'password' => $this->passwordRules(),
])->validateWithBag('updatePassword');
auth()->user()->forceFill([
'password' => Hash::make($this->state['password']),
])->save();
$this->state = [
'current_password' => '',
'password' => '',
'password_confirmation' => '',
];
$this->dispatch('saved');
}
}
}