I have a website built with Laravel 6. When a user updates his e-mail address in the edit profile section in a form and submits it I want to send a link to his original e-mail address so I know he really is the owner of that account and that he has access to that e-mail address. After he clicks a link in the e-mail he received, then the database gets updated with the new e-mail and "email_verified_at" becomes null as he has to confirm the new e-mail address also.
I have created the mail, I can send the e-mail when the user submits the form but I'm clueless on how to create a link, maybe with a token, and when the user clicks it in his e-mail, the database gets updated with the new e-mail address.
I need some suggestions on how I can create events/listeners so that the form is submitted to update the database after the user clicks on the confirmation link.
My ProfileController update function:
public function update(ProfileRequest $request)
{
$data = request();
// when the user submits the form the e-mail is sent.
Mail::to(auth()->user()->email)->send(new ChangeProfileEmail($data));
// line of code to execute after user clicks on link on mail
auth()->user()->update(['name' => $request->get('name'), 'email' => $request->get('email')]);
return back()->withStatus(__('An e-mail was sent to your old e-mail address to confirm these changes.'));
}
The e-mail template in which I should add the link.
@component('mail::message')
<h3>Hello, {{ auth()->user()->name }}</h3>
<p>If you want to change your email from {{ auth()->user()->email }} to {{ $data['email'] }}, please press the Change e-mail button</p>
@component('mail::button', ['url' => ''])
Change e-mail
@endcomponent
@endcomponent
And my form on the edit profile page
<form method="post" action="{{ route('profile.update') }}" autocomplete="off" enctype="multipart/form-data">
@csrf
@method('put')
<h6 class="heading-small text-muted mb-4">{{ __('User information') }}</h6>
@include('alerts.success')
@include('alerts.error_self_update', ['key' => 'not_allow_profile'])
<div class="pl-lg-4">
<div class="form-group{{ $errors->has('name') ? ' has-danger' : '' }}">
<label class="form-control-label" for="input-name">{{ __('Name') }}</label>
<input type="text" name="name" id="input-name" class="form-control{{ $errors->has('name') ? ' is-invalid' : '' }}" placeholder="{{ __('Name') }}" value="{{ old('name', auth()->user()->name) }}" required autofocus>
@include('alerts.feedback', ['field' => 'name'])
</div>
<div class="form-group{{ $errors->has('email') ? ' has-danger' : '' }}">
<label class="form-control-label" for="input-email">{{ __('Email') }}</label>
<input type="email" name="email" id="input-email" class="form-control{{ $errors->has('email') ? ' is-invalid' : '' }}" placeholder="{{ __('Email') }}" value="{{ old('email', auth()->user()->email) }}" required>
@include('alerts.feedback', ['field' => 'email'])
</div>
<div class="text-center">
<button type="submit" class="btn btn-success mt-4">{{ __('Save') }}</button>
</div>
</div>
</form>