【问题标题】:Update user's profile with new e-mail after confirmation link is sent and is clicked on the original e-mail发送确认链接并单击原始电子邮件后,使用新电子邮件更新用户的个人资料
【发布时间】:2020-01-28 02:52:43
【问题描述】:

我有一个使用 Laravel 6 构建的网站。当用户在表单的编辑个人资料部分更新他的电子邮件地址并提交时,我想发送一个指向他原始电子邮件地址的链接,这样我就知道他真的是该帐户的所有者,并且他可以访问该电子邮件地址。在他点击他收到的电子邮件中的链接后,数据库会更新为新的电子邮件,并且“email_verified_at”变为空,因为他还必须确认新的电子邮件地址。

我已经创建了邮件,我可以在用户提交表单时发送电子邮件,但我不知道如何创建链接,可能使用令牌,以及当用户在他的电子邮件中单击它时,数据库将使用新的电子邮件地址进行更新。

我需要一些关于如何创建事件/侦听器的建议,以便在用户单击确认链接后提交表单以更新数据库。

我的 ProfileController 更新功能:

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.'));
}

我应该在其中添加链接的电子邮件模板。

@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

还有我在编辑个人资料页面上的表单

<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>

【问题讨论】:

  • 向他们发送signed url,以便只有该用户可以访问该唯一网址一次以验证他们的电子邮件。

标签: laravel email events


【解决方案1】:

您需要创建一个唯一/签名的 URL。 docs 很好地解释了这一点。 此外,您很可能确实希望创建一个可以在几个小时内临时访问的链接。

基本的做法是这样做:

$url = URL::temporarySignedRoute('register', now()->addDays(2), ['email' => 'user@example.com']);

此 SignedRoute 将在 2 天后到期。

现在您需要在邮件中使用此自定义临时链接。

例如在你的邮件类中这样。

public function toMail($notifiable)
{
    $url = URL::temporarySignedRoute('register', now()->addDays(2), [
        'email' => 'user@example.com', 
        'user' => $this->user->id
    ]);

    return (new MailMessage)
                ->subject('Activate your email address')
                ->line('In order to use the application, please verify your email address.')
                ->action('Activate your account', $url)
                ->line('Thank you for using our application!');
}

【讨论】:

    猜你喜欢
    • 2016-04-21
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 2011-07-19
    • 2020-09-03
    • 2013-02-17
    • 1970-01-01
    • 2018-05-06
    相关资源
    最近更新 更多