Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

need email_verified_at set null first to using newEmail()? #14

Open
YugoSamakuhaku opened this issue Mar 25, 2022 · 0 comments
Open

need email_verified_at set null first to using newEmail()? #14

YugoSamakuhaku opened this issue Mar 25, 2022 · 0 comments

Comments

@YugoSamakuhaku
Copy link

I use livewire, when updating an email I use

   if ($this->email != $this->user->email) {
             $this->user->email_verified_at = null;
             $this->user->newEmail($this->user->email);
  }

if email_verified_at is not set to null, newEmail() will not create a new record in the pending_user_emails table

this is my edit file

class Edit extends Component {
    public User $user;

    public $roles          = [];
    public $listsForFields = [];

    public $password              = '';
    public $password_confirmation = '';

    public $email = '';

    public function mount(User $user) {
        $this->user  = $user;
        $this->email = $this->user->email;
        $this->roles = $this->user->roles()->pluck('id')->toArray();
        $this->initListsForFields();
    }

    public function render() {
        return view('livewire.user.edit');
    }

    public function submit() {
        $this->validate();
        $this->user->password = $this->password;

        if ($this->email != $this->user->email) {
            $this->user->email_verified_at = null;
            $this->user->newEmail($this->user->email);
        } else {
            $this->user->save();
        }
        $this->user->roles()->sync($this->roles);

        return redirect()->route('users.index');
    }

    protected function rules() {
        return [
            'user.name'             => ['required', 'string', 'max:255'],
            'user.email'            => ['required', 'email', 'max:255', 'unique:users,email,' . $this->user->id],
            'user.biography'        => ['nullable'],
            'roles'                 => ['required', 'array'],
            'roles.*.id'            => ['integer', 'exists:roles,id'],
            'password'              => ['string', 'min:8', 'confirmed'],
            'password_confirmation' => ['string', 'min:8'],
        ];
    }

    public function initListsForFields(): void {
        $this->listsForFields['roles'] = Role::pluck('name', 'id')->toArray();
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant